Converts the specified value to another datatype or a different datetime display format.
convert (datatype [(length) | (precision[, scale])] [null | not null], expression [, style])
When Java is enabled in the database, datatype can also be a Java-SQL class in the current database.
When Java is enabled in the database, expression can be a value to be converted to a Java-SQL class.
When unichar is used as the destination datatype, the default length of 30 Unicode values is used if no length is specified.
When converting datetime or smalldatetime data to a character type, use the style numbers in the following table to specify the display format. Values in the left-most column display 2-digit years (yy). For 4-digit years (yyyy), add 100, or use the value in the middle column.
When converting date data to a character type, use style numbers 1 through 7 (101 through 107) or 10 through 12 (110 through 112) in the following table to specify the display format. The default value is 100 (mon dd yyyy hh:miAM (or PM )). If date data is converted to a style that contains a time portion, that time portion reflects the default value of zero.
When converting time data to a character type, use style number 8 or 9 (108 or 109) to specify the display format. The default is 100 (mon dd yyyy hh:miAM (or PM)). If time data is converted to a style that contains a date portion, the default date of Jan 1, 1900 is displayed.
Without Century (yy) |
With Century (yyyy) |
Standard |
Output |
---|---|---|---|
- |
0 or 100 |
Default |
mon dd yyyy hh:mm AM (or PM) |
1 |
101 |
USA |
mm/dd/yy |
2 |
2 |
SQL standard |
yy.mm.dd |
3 |
103 |
English/French |
dd/mm/yy |
4 |
104 |
German |
dd.mm.yy |
5 |
105 |
dd-mm-yy |
|
6 |
106 |
dd mon yy |
|
7 |
107 |
mon dd, yy |
|
8 |
108 |
HH:mm:ss |
|
- |
9 or 109 |
Default + milliseconds |
mon dd yyyy hh:mm:ss AM (or PM) |
10 |
110 |
USA |
mm-dd-yy |
11 |
111 |
Japan |
yy/mm/dd |
12 |
112 |
ISO |
yymmdd |
13 |
113 |
yy/dd/mm |
|
14 |
114 |
mm/yy/dd |
|
14 |
114 |
hh:mi:ss:mmmAM(or PM) |
|
15 |
115 |
dd/yy/mm |
|
- |
16 or 116 |
mon dd yyyy HH:mm:ss |
|
17 |
117 |
hh:mmAM |
|
18 |
118 |
HH:mm |
|
19 |
hh:mm:ss:zzzAM |
||
20 |
hh:mm:ss:zzz |
||
21 |
yy/mm/dd HH:mm:ss |
||
22 |
yy/mm/dd HH:mm AM (or PM) |
||
23 |
yyyy-mm-ddTHH:mm:ss |
||
36 |
136 |
hh:mm:ss.zzzzzzAM(PM) |
|
37 |
137 |
hh:mm.ss.zzzzzz |
|
38 |
138 |
mon dd yyyy hh:mm:ss.zzzzzzAM(PM) |
|
39 |
139 |
mon dd yyyy hh:mm:ss.zzzzzz |
|
40 |
140 |
yyyy-mm-dd hh:mm:ss.zzzzzz |
“mon” indicates a month spelled out, “mm” the month number or minutes. “HH ”indicates a 24-hour clock value, “hh” a 12-hour clock value. The last row, 23, includes a literal “T” to separate the date and time portions of the format.Styles 24–35 are undefined.
The default values (style 0 or 100), and style 9 or 109 return the century (yyyy). When converting to char or varchar from smalldatetime, styles that include seconds or milliseconds show zeros in those positions.
select title, convert(char(12), total_sales) from titles
select title, total_sales from titles where convert(char(20), total_sales) like "1%"
select convert(char(12), getdate(), 3)
select convert(varchar(12), pubdate, 3) from titles
select convert(integer, 0x00000100)
select convert (binary, 10)
select convert(bit, $1.11)
select title, convert (char(100) not null, total_sales) into #tempsales from titles
ANSI SQL – Compliance level: Transact-SQL extension.
Any user can execute convert.