MS SQL Server Convert DateTime to String

I regularly use a SQL statement to extract a datetime, as I feel like it formats it faster than code. Plus, if you update the source, you only have to do it once, as opposed to doing it in every place that it gets displayed.

Here is the conversion number to use in the CONVERT statement.

CONVERT(VARCHAR(30), [DateValue], [Style])
Style Output
0 Sep 11 2001  8:52AM
1 09/11/01
2 01.09.11
3 11/09/01
4 11.09.01
5 11-09-01
6 11 Sep 01
7 Sep 11, 01
8 08:52:21
9 Sep 11 2001  8:52:21:000AM
10 09-11-01
11 01/09/11
12 010911
13 11 Sep 2001 08:52:21:000
14 08:52:21:000
20 2001-09-11 08:52:21
21 2001-09-11 08:52:21.000
22 09/11/01  8:52:21 AM
23 2001-09-11
24 08:52:21
25 2001-09-11 08:52:21.000
100 Sep 11 2001  8:52AM
101 09/11/2001
102 2001.09.11
103 11/09/2001
104 11.09.2001
105 11-09-2001
106 11 Sep 2001
107 Sep 11, 2001
108 08:52:21
109 Sep 11 2001  8:52:21:000AM
110 09-11-2001
111 2001/09/11
112 20010911
113 11 Sep 2001 08:52:21:000
114 08:52:21:000
120 2001-09-11 08:52:21
121 2001-09-11 08:52:21.000
126 2001-09-11T08:52:21
127 2001-09-11T08:52:21
130 23 ????? ??????? 1422  8:52:21
131 23/06/1422  8:52:21:000AM

Also, if you happened to notice the date: Yes, it is the time that the first plane hit the first world trade center… Never Forget.

I’m still coding, so we are winning!

You can also view more details on the MSDN site here.

RJ writes custom Windows and Web applications using the Microsoft .NET framework, and enjoys solving problems with code, often while listening to a techno soundcloud stream. He hopes to one day write an application that many businesses will use.

Twitter 

Comments are closed.