Monday 29 July 2019

Convert Datetime To String

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:
  • VARCHAR is the first argument that represents the string type.
  • datetime is an expression that evaluates to date or datetime value that you want to convert to a string
  • sytle specifies the format of the date. The value of style is a number predefined by SQL Server. The style parameter 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)StandardFormat
0 or 100Default for datetime and smalldatetimemon dd yyyy hh:miAM (or PM)
1101U.S.1 = mm/dd/yy
101 = mm/dd/yyyy
2102ANSI2 = yy.mm.dd
102 = yyyy.mm.dd
3103British/French3 = dd/mm/yy
103 = dd/mm/yyyy
4104German4 = dd.mm.yy
104 = dd.mm.yyyy
5105Italian5 = dd-mm-yy
105 = dd-mm-yyyy
61066 = dd mon yy
106 = dd mon yyyy
71077 = Mon dd, yy
107 = Mon dd, yyyy
8108hh:mi:ss
9 or 109Default + millisecondsmon dd yyyy hh:mi:ss:mmmAM (or PM)
10110USA10 = mm-dd-yy
110 = mm-dd-yyyy
11111JAPAN11 = yy/mm/dd
111 = yyyy/mm/dd
12112ISO12 = yymmdd
112 = yyyymmdd
13 or 113Europe default + millisecondsdd mon yyyy hh:mi:ss:mmm(24h)
14114hh:mi:ss:mmm(24h)
20 or 120ODBC canonicalyyyy-mm-dd hh:mi:ss(24h)
21 or 121ODBC canonical (with milliseconds) default for time, date, datetime2, and datetimeoffsetyyyy-mm-dd hh:mi:ss.mmm(24h)
126ISO8601yyyy-mm-ddThh:mi:ss.mmm (no spaces)
127ISO8601 with time zone Z.yyyy-mm-ddThh:mi:ss.mmmZ (no spaces)
130Hijridd mon yyyy hh:mi:ss:mmmAM
131Hijridd/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:

No comments:

Post a Comment