How to get different date formats in SQL Server

 How to get different date formats in SQL Server

  1. Use the SELECT statement with CONVERT function and date format option for the date values needed
  2. To get YYYY-MM-DD use this T-SQL syntax SELECT CONVERT(varchar, getdate(), 23)
  3. To get MM/DD/YY use this T-SQL syntax SELECT CONVERT(varchar, getdate(), 1)
  4. Check out the chart to get a list of all format options

Below is a list of SQL date formats and an example of the output.  The date used for all of these examples is "2006-12-30 00:38:54.840".

DATE ONLY FORMATS
Format #QueryFormatSample
1select convert(varchar, getdate(), 1)mm/dd/yy12/30/06
2select convert(varchar, getdate(), 2)yy.mm.dd06.12.30
3select convert(varchar, getdate(), 3)dd/mm/yy30/12/06
4select convert(varchar, getdate(), 4)dd.mm.yy30.12.06
5select convert(varchar, getdate(), 5)dd-mm-yy30-12-06
6select convert(varchar, getdate(), 6)dd-Mon-yy30 Dec 06
7select convert(varchar, getdate(), 7)Mon dd, yyDec 30, 06
10select convert(varchar, getdate(), 10)mm-dd-yy12-30-06
11select convert(varchar, getdate(), 11)yy/mm/dd06/12/30
12select convert(varchar, getdate(), 12)yymmdd061230
23select convert(varchar, getdate(), 23)yyyy-mm-dd2006-12-30
101select convert(varchar, getdate(), 101)mm/dd/yyyy12/30/2006
102select convert(varchar, getdate(), 102)yyyy.mm.dd2006.12.30
103select convert(varchar, getdate(), 103)dd/mm/yyyy30/12/2006
104select convert(varchar, getdate(), 104)dd.mm.yyyy30.12.2006
105select convert(varchar, getdate(), 105)dd-mm-yyyy30-12-2006
106select convert(varchar, getdate(), 106)dd Mon yyyy30 Dec 2006
107select convert(varchar, getdate(), 107)Mon dd, yyyyDec 30, 2006
110select convert(varchar, getdate(), 110)mm-dd-yyyy12-30-2006
111select convert(varchar, getdate(), 111)yyyy/mm/dd2006/12/30
112select convert(varchar, getdate(), 112)yyyymmdd20061230
 
TIME ONLY FORMATS
8select convert(varchar, getdate(), 8)hh:mm:ss00:38:54
14select convert(varchar, getdate(), 14)hh:mm:ss:nnn00:38:54:840
24select convert(varchar, getdate(), 24)hh:mm:ss00:38:54
108select convert(varchar, getdate(), 108)hh:mm:ss00:38:54
114select convert(varchar, getdate(), 114)hh:mm:ss:nnn00:38:54:840
 
DATE & TIME FORMATS
0select convert(varchar, getdate(), 0)Mon dd yyyy hh:mm AM/PMDec 30 2006 12:38AM
9select convert(varchar, getdate(), 9)Mon dd yyyy hh:mm:ss:nnn AM/PMDec 30 2006 12:38:54:840AM
13select convert(varchar, getdate(), 13)dd Mon yyyy hh:mm:ss:nnn AM/PM30 Dec 2006 00:38:54:840AM
20select convert(varchar, getdate(), 20)yyyy-mm-dd hh:mm:ss2006-12-30 00:38:54
21select convert(varchar, getdate(), 21)yyyy-mm-dd hh:mm:ss:nnn2006-12-30 00:38:54.840
22select convert(varchar, getdate(), 22)mm/dd/yy hh:mm:ss AM/PM12/30/06 12:38:54 AM
25select convert(varchar, getdate(), 25)yyyy-mm-dd hh:mm:ss:nnn2006-12-30 00:38:54.840
100select convert(varchar, getdate(), 100)Mon dd yyyy hh:mm AM/PMDec 30 2006 12:38AM
109select convert(varchar, getdate(), 109)Mon dd yyyy hh:mm:ss:nnn AM/PMDec 30 2006 12:38:54:840AM
113select convert(varchar, getdate(), 113)dd Mon yyyy hh:mm:ss:nnn30 Dec 2006 00:38:54:840
120select convert(varchar, getdate(), 120)yyyy-mm-dd hh:mm:ss2006-12-30 00:38:54
121select convert(varchar, getdate(), 121)yyyy-mm-dd hh:mm:ss:nnn2006-12-30 00:38:54.840
126select convert(varchar, getdate(), 126)yyyy-mm-dd T hh:mm:ss:nnn2006-12-30T00:38:54.840
127select convert(varchar, getdate(), 127)yyyy-mm-dd T hh:mm:ss:nnn2006-12-30T00:38:54.840
 
ISLAMIC CALENDAR DATES
130select convert(nvarchar, getdate(), 130)dd mmm yyyy hh:mi:ss:nnn AM/PMdate output
131select convert(nvarchar, getdate(), 131)dd mmm yyyy hh:mi:ss:nnn AM/PM10/12/1427 12:38:54:840AM

You can also format the date or time without dividing characters, as well as concatenate the date and time string:

Sample statementFormatOutput
select replace(convert(varchar, getdate(),101),'/','')mmddyyyy12302006
select replace(convert(varchar, getdate(),101),'/','') + replace(convert(varchar, getdate(),108),':','')mmddyyyyhhmmss12302006004426

Comments

Popular posts from this blog

Uploading Image to Sql Server Image Datatype in asp.net using fileupload Control

Get Running Sum of Query SQL Query