SQL CONVERT () function usage

Source: Internet
Author: User
Tags datetime getdate odbc string format

CONVERT function format in SQL:

CONVERT (data_type, expression [, style])

Parameter description:

Expression

Is any valid Microsoft® SQL Server™ expression ..

Data_type

The data types provided by the target system, including bigint and SQL _variant. User-defined data types cannot be used.


Length

Optional parameters of the nchar, nvarchar, char, varchar, binary, or varbinary data type.

Style

Date format style, which converts datetime or smalldatetime data to character data (nchar, nvarchar, char, varchar, nchar, or nvarchar data type); or string format style, to convert float, real, money, or smallmoney data to character data (nchar, nvarchar, char, varchar, nchar, or nvarchar data type ).

Remarks

The conversion time of a style number is as follows:

The conversion time of a style number is as follows:

 

Without Century Digital (yy) Digital Century (yyyy)
Standard

Input/output **
- 0 or 100 (*) Default value Mon dd yyyy hh: miAM (or PM)
1 101 USA Mm/dd/yyyy
2 102 ANSI Yy. mm. dd
3 103 UK/France Dd/mm/yy
4 104 Germany Dd. mm. yy
5 105 Italy Dd-mm-yy
6 106 - Dd mon yy
7 107 - Mon dd, yy
8 108 - Hh: mm: ss
- 9 or 109 (*) Default value + millisecond Mon dd yyyy hh: mi: ss: mmmAM (or PM)
10 110 USA Mm-dd-yy
11 111 Japan Yy/mm/dd
12 112 ISO Yymmdd
- 13 or 113 (*) European default value + millisecond Dd mon yyyy hh: mm: ss: mmm (24 h)
14 114 - Hh: mi: ss: mmm (24 h)
- 20 or 120 (*) ODBC specifications Yyyy-mm-dd hh: mm: ss [. fff]
- 21 or 121 (*) ODBC specifications (in milliseconds) Yyyy-mm-dd hh: mm: ss [. fff]
- 126 (***) ISO8601 Yyyy-mm-dd Thh: mm: ss. mmm (excluding spaces)
- 130 * Hijri **** Dd mon yyyy hh: mi: ss: mmmAM
- 131 * Hijri **** Dd/mm/yy hh: mi: ss: mmmAM

CONVERT (data_type, expression_r_r [, style])

Statement and query result:
Select convert (varchar (100), GETDATE (), 0): 05 16 2006 AM
Select convert (varchar (100), GETDATE (), 1): 05/16/06
Select convert (varchar (100), GETDATE (), 2): 06.05.16
Select convert (varchar (100), GETDATE (), 3): 16/05/06
Select convert (varchar (100), GETDATE (), 4): 16.05.06
Select convert (varchar (100), GETDATE (), 5): 16-05-06
Select convert (varchar (100), GETDATE (), 6): 16 05 06
Select convert (varchar (100), GETDATE (), 7): 05 16, 06
Select convert (varchar (100), GETDATE (), 8): 10:57:46
Select convert (varchar (100), GETDATE (), 9): 05 16 2006 10: 57: 46: 827AM
Select convert (varchar (100), GETDATE (), 10): 05-16-06
Select convert (varchar (100), GETDATE (), 11): 06/05/16
Select convert (varchar (100), GETDATE (), 12): 060516
Select convert (varchar (100), GETDATE (), 13): 16 05 2006 10: 57: 46: 937
Select convert (varchar (100), GETDATE (), 14): 10: 57: 46: 967
Select convert (varchar (100), GETDATE (), 20): 10:57:47
Select convert (varchar (100), GETDATE (), 21): 10:57:47. 157
Select convert (varchar (100), GETDATE (), 22): 05/16/06 10:57:47 AM
Select convert (varchar (100), GETDATE (), 23):
Select convert (varchar (100), GETDATE (), 24): 10:57:47
Select convert (varchar (100), GETDATE (), 25): 10:57:47. 250
Select convert (varchar (100), GETDATE (), 100): 05 16 2006 AM
Select convert (varchar (100), GETDATE (), 101): 05/16/2006
Select convert (varchar (100), GETDATE (), 102): 2006.05.16
Select convert (varchar (100), GETDATE (), 103): 16/05/2006
Select convert (varchar (100), GETDATE (), 104): 16.05.2006
Select convert (varchar (100), GETDATE (), 105): 16-05-2006
Select convert (varchar (100), GETDATE (), 106): 16 05 2006
Select convert (varchar (100), GETDATE (), 107): 05 16,200 6
Select convert (varchar (100), GETDATE (), 108): 10:57:49
Select convert (varchar (100), GETDATE (), 109): 05 16 2006 10: 57: 49: 437AM
Select convert (varchar (100), GETDATE (), 110): 05-16-2006
Select convert (varchar (100), GETDATE (), 111): 2006/05/16
Select convert (varchar (100), GETDATE (), 112): 20060516
Select convert (varchar (100), GETDATE (), 113): 16 05 2006 10: 57: 49: 513
Select convert (varchar (100), GETDATE (), 114): 10: 57: 49: 547
Select convert (varchar (100), GETDATE (), 120): 10:57:49
Select convert (varchar (100), GETDATE (), 121): 10:57:49. 700
Select convert (varchar (100), GETDATE (), 126): 2006-05-16T10: 57: 49.827
Select convert (varchar (100), GETDATE (), 130): 18 ???? ?????? 1427 10: 57: 49: 907AM
Select convert (varchar (100), GETDATE (), 131): 18/04/1427 10: 57: 49: 920AM

Note:
This style is generally in the time type (datetime, smalldatetime) and string type (nchar, nvarchar, char, varchar)
It is used only when they are converted to each other.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.