[SQL Server] CONVERT date in MS SQL Server format Daquan

CONVERT function
to a data type of an expression explicitly converted to another data type.
SQL Server in the date format.

SQL Server supports the use of the Kuwaiti algorithm data format in Arabic style.

In the table, the left side shows a two datetime or smalldatetime to character data of the style value. To the style value plus 100, including the availability of digital century four-digit year (yyyy).

Without digital century (yy) With digital century (yyyy) standard input Output**
- 100 or 0 (*) Defaults mon dd yyyy hh:miAM(或 PM)
1 101 United States 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 ms + mon dd yyyy hh:mi:ss:mmmAM(或PM)
10 110 United States mm-dd-yy
11 111 Japan yy/mm/dd
12 112 ISO yymmdd
- 113 or 13 (*) European defaults ms + dd mon yyyy hh:mm:ss:mmm(24h)
14 114 - hh:mi:ss:mmm(24h)
- 120 or 20 (*) ODBC specification yyyy-mm-dd hh:mm:ss[.fff]
- 121 or 21 (*) ODBC specification (band ms) yyyy-mm-dd hh:mm:ss[.fff]
- 126(***) ISO8601 yyyy-mm-dd Thh: mm: ss: mmm (without spaces)
- 130* Kuwait dd mon yyyy hh:mi:ss:mmmAM
- 131* Kuwait dd/mm/yy hh:mi:ss:mmmAM

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

* Default values ( style 0 or 100,9 or 109,13 or 113,20 or 120,21 or 121) always returns the digital century (yyyy).
** When converted to datetime input; output when converted to character data.
*** devoted to xml . For the datetime or smalldatetime to character data conversion output format as shown in Table. For from a float , Money or smallmoney to character data conversion output is equivalent to style 2. For the real to character data conversion, output is equivalent to style . 1.


Important   By default, SQL Server 2049 interprets two-digit year according to the cutoff year. I.e., two-digit year 2049 is interpreted as 49, 50 and the two-digit year is interpreted as 1950. Many client applications (such as those based client application OLE Automation object) uses 2030 as the cutoff year. SQL Server provides a configuration option ( "two-digit year cutoff"), in order to change the date and year of closing are using SQL Server process consistency. However, the safest way is to specify the four-digit year.

 

When the smalldatetime when character data is converted, the pattern comprising seconds or milliseconds show zeros in these positions. When the datetime or smalldatetime when converting values, by using an appropriate char or varchar to cut unnecessary portion of the date data type length.

The following table shows the float or real time data is converted into character style value.

 

value Export
0 (default value) Up to 6 digits. According to require the use of scientific notation.
1 Always 8-bit value. Always use scientific notation.
2 Always 16-bit value. Always use scientific notation.


 

 

 

 

In the following table, the left column represents money or smallmoney converted to character data style value.

value Export
0 (default value) No separation between the left of the decimal comma every three digits, taking right of the decimal digits, e.g. 4235.98.
1 Between each of the three left of the decimal numbers separated by commas, taking right of the decimal digits, for example, 3,510.92.
2 No separation between the left of the decimal comma every three digits, four digits taken right of the decimal, e.g. 4235.9819.

 

 

 

 

 

Use CONVERT:

CONVERT (data_type[(length)], exPRession [, style])


select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08

select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),\'-\',\'\'),\' \',\'\'),\':\',\'\')
20040912110608

select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12

select CONVERT(varchar(12) , getdate(), 112 )
20040912

select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12

select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004

select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004

select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004

select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004

select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004

select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004

select CONVERT(varchar(12) , getdate(), 108 )
11:06:08

select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1

select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004

select CONVERT(varchar(12) , getdate(), 113 )
12 09 2004 1

select CONVERT(varchar(12) , getdate(), 114 )
11:06:08.177

 

Reproduced in: https: //www.cnblogs.com/Alenliu/p/3916029.html

Guess you like

Origin blog.csdn.net/weixin_34351321/article/details/93470033