One of the more frequent questions I get for SQL Server is how to format a DATETIME into a specific date format. Here’s a summary of the different date formats that come standard in SQL Server as part of the CONVERT function. Following the standard date formats are some extended date formats that are often requested by SQL Server developers. It is worth to note that the outputs of these conversions are VARCHAR and not DATETIME. With this in mind, any date comparisons performed after the DATETIME value has been formatted are using the VARCHAR value of the date and time and not its original DATETIME value.
Date Format | SQL Statement | Sample Output |
Mon DD YYYY 1 HH:MIAM (or PM) | SELECT CONVERT(VARCHAR(20), GETDATE(), 100) |
Jan 1 2010 1:29PM 1 |
MM/DD/YY | SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS |
11/23/10 |
MM/DD/YYYY | SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS |
11/23/2010 |
YY.MM.DD | SELECT CONVERT(VARCHAR(8), GETDATE(), 2) AS |
10.01.01 |
YYYY.MM.DD | SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS |
2010.01.01 |
DD/MM/YY | SELECT CONVERT(VARCHAR(8), GETDATE(), 3) AS |
20/02/10 |
DD/MM/YYYY | SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS |
20/02/2010 |
DD.MM.YY | SELECT CONVERT(VARCHAR(8), GETDATE(), 4) AS |
25.12.10 |
DD.MM.YYYY | SELECT CONVERT(VARCHAR(10), GETDATE(), 104) AS |
25.12.2010 |
DD-MM-YY | SELECT CONVERT(VARCHAR(8), GETDATE(), 5) AS |
24-01-10 |
DD-MM-YYYY | SELECT CONVERT(VARCHAR(10), GETDATE(), 110) AS |
24-01-2010 |
DD Mon YY 1 | SELECT CONVERT(VARCHAR(9), GETDATE(), 6) AS [DD MON |
04 Jul 06 1 |
DD Mon YYYY 1 | SELECT CONVERT(VARCHAR(11), GETDATE(), 106) AS [DD MON |
04 Jul 2010 1 |
Mon DD, YY 1 | SELECT CONVERT(VARCHAR(10), GETDATE(), 7) AS [Mon DD, |
Jan 24, 10 1 |
Mon DD, YYYY 1 | SELECT CONVERT(VARCHAR(12), GETDATE(), 107) AS [Mon DD, |
Jan 24, 2010 1 |
HH:MM:SS | SELECT CONVERT(VARCHAR(8), GETDATE(), 108) |
03:24:53 |
Mon DD YYYY HH:MI:SS:MMMAM (or PM) 1 | SELECT CONVERT(VARCHAR(26), GETDATE(), 109) |
Apr 28 2010 12:32:29:253PM 1 |
MM-DD-YY | SELECT CONVERT(VARCHAR(8), GETDATE(), 10) AS |
01-01-06 |
MM-DD-YYYY | SELECT CONVERT(VARCHAR(10), GETDATE(), 110) AS |
01-01-2010 |
YY/MM/DD | SELECT CONVERT(VARCHAR(8), GETDATE(), 11) AS |
10/11/23 |
YYYY/MM/DD | SELECT CONVERT(VARCHAR(10), GETDATE(), 111) AS |
2010/11/23 |
YYMMDD | SELECT CONVERT(VARCHAR(6), GETDATE(), 12) AS |
100124 |
YYYYMMDD | SELECT CONVERT(VARCHAR(8), GETDATE(), 112) AS |
20100124 |
DD Mon YYYY HH:MM:SS:MMM(24h) 1 | SELECT CONVERT(VARCHAR(24), GETDATE(), 113) |
28 Apr 2010 00:34:55:200 1 |
HH:MI:SS:MMM(24H) | SELECT CONVERT(VARCHAR(12), GETDATE(), 114) AS |
11:34:23:013 |
YYYY-MM-DD HH:MI:SS(24h) | SELECT CONVERT(VARCHAR(20), GETDATE(), 120) |
2010-01-01 13:42:24 |
YYYY-MM-DD HH:MI:SS.MMM(24h) | SELECT CONVERT(VARCHAR(23), GETDATE(), 121) |
2010-02-20 06:35:24.489 |
YYYY-MM-DDTHH:MM:SS:MMM | SELECT CONVERT(VARCHAR(23), GETDATE(), 126) |
2010-11-23T11:25:43:250 |
DD Mon YYYY HH:MI:SS:MMMAM 1 | SELECT CONVERT(VARCHAR(26), GETDATE(), 130) |
28 Apr 2010 12:39:32:429AM 1 |
DD/MM/YYYY HH:MI:SS:MMMAM | SELECT CONVERT(VARCHAR(25), GETDATE(), 131) |
28/04/2010 12:39:32:429AM |
>
>Custom Formats:
1 To make the month name in upper case, simply use the UPPER |
References: MSDN