Convert Date Formats
SELECT CONVERT (data_type(length)),Date, DateFormatCode)
- Data_Type - We need to define data type along with length. In the date function, we use Varchar(length) data types.
- Date - We need to specify the date that we want to convert.
- DateFormatCode - We need to specify DateFormatCode to convert a date in an appropriate form. We will explore more on this in the upcoming section.
Date format option & SQL convert date output
- 0 - [MMM DD YYYY hh:mm(AM/PM)]
- 1 - [MM/DD/YY] (Standard: U.S.A.)
- 2 - [YY.MM.DD] (Standard: ANSI)
- 3 - [DD/MM/YY] (Standard: British/French)
- 4 - [DD.MM.YY] (Standard: German)
- 5 - [DD-MM-YY] (Standard: Italian)
- 6 - [DD MMM YY] (Standard: Shortened month name)
- 7 - [MMM DD, YY] (Standard: Shortened month name)
- 8 - [HH:MM: SS] (Standard: 24 hour time)
- 9 - [MMM DD YYYY hh:mm:ss:mmm(AM/PM)] (Standard: Default + milliseconds)
- 10 - [MM-DD-YY] (Standard: USA)
- 11 - [YY/MM/DD] (Standard: JAPAN)
- 12 - [YYMMDD] (Standard: ISO)
- 13 - [DD MMM YYYY HH:MM:SS:MMM] (Standard: Europe default + milliseconds)
- 14 - [HH:MM:SS:MMM] (Standard: 24 hour time with milliseconds)
- 20 - [YYYY-MM-DD HH:MM:SS] (Standard: ODBC canonical)
- 21 - [YYYY-MM-DD HH:MM:SS.mmm] (Standard: ODBC canonical with milliseconds)
- 22 - [mm/dd/yy hh:mm:ss (AM/PM)] (Standard: USA with Time AM/PM)
- 23 - [yyyy-mm-dd]
- 24 - [hh:mm:ss]
- 25 - [mm:ss.m]
- 26 - [yyyy-dd-mm hh:mm:ss.mmm]
- 27 - [mm-dd-yyyy hh:mm:ss.mmm]
- 28 - [mm-yyyy-dd hh:mm:ss.mmm]
- 29 - [mm:ss.m]
- 30 - [dd-yyyy-mm hh:mm:ss.mmm]
- 31 - [yyyy-dd-mm]
- 32 - [mm-dd-yyyy]
- 33 - [mm-yyyy-dd]
- 34 - [dd/mm/yyyy]
- 35 - [dd-yyyy-mm]
- 100 - [MMM DD YYYY HH: SS (AM/PM)] (Standard: Default)
- 101 - [MM/DD/YYYY] (Standard: USA)
- 102 - [YYYY.MM.DD] (Standard: ANSI)
- 103 - [DD/MM/YYYY] (Standard: British / French)
- 104 - [DD.MM.YY] (Standard: German)
- 105 - [DD-MM-YY] (Standard: Italian)
- 106 - [DD MMM YYYY] (Standard: Shortened month name)
- 107 - [MMM DD,YYYY] (Standard: Shortened month name)
- 108 - [HH:MM: SS] (Standard: 24 hour time)
- 109 - [MMM DD YYYY hh:mm:ss:mmm(AM/PM)] (Standard: Default + milliseconds)
- 110 - [MM- DD-YY] (Standard: USA)
- 111 - [YYYY/MM/DD] (Standard: JAPAN)
- 112 - [YYYYMMDD] (Standard: ISO)
- 113 - [DD MMM YYYY HH:MM:SS: MMM] (Standard: Europe default + milliseconds)
- 114 - [HH:MM:SS: MMM] (Standard: 24 hour time with milliseconds)
- 120 - [YYYY-MM-DD HH:MM: SS] (Standard: ODBC canonical)
- 121 - [YYYY-MM-DD HH:MM: SS.mmm] (Standard: ODBC canonical with milliseconds)
- 126 - [YYYY-MM-DDTHH:MM: SS.mmm] (Standard: ISO8601)
- 127 - [YYYY-MM-DDTHH:MM: SS.mmm]
- 130 - [DD MMM YYYY hh:mi:ss:mmm(AM/PM)] (Standard: Islamic/Hijri date)
- 131 - [DD MMM YYYY hh:mm:ss:mmm(AM/PM)]