Summary: in this tutorial, you will learn how to convert datetime to string in SQL Server using the
CONVERT() function.
Using the CONVERT() function to convert datetime to string
To convert a datetime to a string, you use the
CONVERT() function as follows:
In this syntax:
VARCHARis the first argument that represents the string type.datetimeis an expression that evaluates to date or datetime value that you want to convert to a stringsytlespecifies the format of the date. The value of style is a number predefined by SQL Server. Thestyleparameter is optional.
The following table illustrates the valid style and the corresponding format of the datetime after converting to a string.
| Without century (yy) | With century (yyyy) | Standard | Format |
|---|---|---|---|
| – | 0 or 100 | Default for datetime and smalldatetime | mon dd yyyy hh:miAM (or PM) |
| 1 | 101 | U.S. | 1 = mm/dd/yy 101 = mm/dd/yyyy |
| 2 | 102 | ANSI | 2 = yy.mm.dd 102 = yyyy.mm.dd |
| 3 | 103 | British/French | 3 = dd/mm/yy 103 = dd/mm/yyyy |
| 4 | 104 | German | 4 = dd.mm.yy 104 = dd.mm.yyyy |
| 5 | 105 | Italian | 5 = dd-mm-yy 105 = dd-mm-yyyy |
| 6 | 106 | – | 6 = dd mon yy 106 = dd mon yyyy |
| 7 | 107 | – | 7 = Mon dd, yy 107 = Mon dd, yyyy |
| 8 | 108 | – | hh:mi:ss |
| – | 9 or 109 | Default + milliseconds | mon dd yyyy hh:mi:ss:mmmAM (or PM) |
| 10 | 110 | USA | 10 = mm-dd-yy 110 = mm-dd-yyyy |
| 11 | 111 | JAPAN | 11 = yy/mm/dd 111 = yyyy/mm/dd |
| 12 | 112 | ISO | 12 = yymmdd 112 = yyyymmdd |
| – | 13 or 113 | Europe default + milliseconds | dd mon yyyy hh:mi:ss:mmm(24h) |
| 14 | 114 | – | hh:mi:ss:mmm(24h) |
| – | 20 or 120 | ODBC canonical | yyyy-mm-dd hh:mi:ss(24h) |
| – | 21 or 121 | ODBC canonical (with milliseconds) default for time, date, datetime2, and datetimeoffset | yyyy-mm-dd hh:mi:ss.mmm(24h) |
| – | 126 | ISO8601 | yyyy-mm-ddThh:mi:ss.mmm (no spaces) |
| – | 127 | ISO8601 with time zone Z. | yyyy-mm-ddThh:mi:ss.mmmZ (no spaces) |
| – | 130 | Hijri | dd mon yyyy hh:mi:ss:mmmAM |
| – | 131 | Hijri | dd/mm/yyyy hh:mi:ss:mmmAM |
Converting a datetime to a string examples
1) Convert datetime to string in mon dd yyyy hh:miAM (or PM) format example
Here is the output:
2) Convert datetime to string in mm/dd/yy and mm/dd/yyyy formats example
Here is the output:
3) Convert datetime to string in mon dd yyyy hh:miAM (or PM) format example
Here is the output:
4) Convert datetime to string in dd.mm.yy and dd.mm.yyyy formats example
Here is the output:
5) Convert datetime to string in dd-mm-yy dd-mm-yyyy formats example
Here is the output:
6) Convert datetime to string in dd mon yy dd mon yyyy formats example
Here is the output:
7) Convert datetime to string in Mon dd, yy Mon dd, yyyy formats example
Here is the output:
8) Convert datetime to string in hh:mi:ss format example
Here is the output:
9) Convert datetime to string in mon dd yyyy hh:mi:ss:mmmAM (or PM) format example
Here is the output:
10) Convert datetime to string in mm-dd-yy and mm-dd-yyyy format example
Here is the result set:
11) Convert datetime to string in yy/mm/dd and yyyy/mm/dd format example
Here is the result set:
12) Convert datetime to string in yymmdd and yyyymmdd format example
Here is the result set:
13) Convert datetime to string in dd mon yyyy hh:mi:ss:mmm(24h) format example
Here is the result set:
14) Convert datetime to string in hh:mi:ss:mmm(24h) format example
Here is the result set:
15) Convert datetime to string in yyyy-mm-dd hh:mi:ss(24h) format example
Here is the result set:
16) Convert datetime to string in yyyy-mm-dd hh:mi:ss.mmm(24h) format example
Here is the result set:
17) Convert datetime to string in yyyy-mm-ddThh:mi:ss.mmm format example
Here is the result set:
18) Convert datetime to string in yyyy-mm-ddThh:mi:ss.mmmZ format example
Here is the result set:
1
2
3
4
5
|
s1
-------------------------
2019-12-31T14:43:35.863
(1 row affected)
|
No comments:
Post a Comment