Tuesday, January 21, 2014

SQL Tips and Tricks - Part 1

Every now and then I have to convert a datetime value to a varchar value in SQL Server and I can never remember which conversion type I want to use.  I used to be able to find a handy list fairly easily, but this last time I came up empty.  So I ran through every combination from 101 to 140 and logged my results.  This is the list I came up with.  There may be more, but this got me through for the time being.  I have more SQL tips and tricks that will come later, but this is a good start.


Code Format Example
101 MM/dd/yyyy 01/08/2014
102 yyyy.MM.dd 2014.01.08
103 dd/MM/yyyy 08/01/2014
104 dd.MM.yyyy 08.01.2014
105 MM-dd-yyyy 08-01-2014
106 dd mon yyyy 08 Jan 2014
107 mon dd, yyyy Jan 08, 2014
108 hh:mm:ss 12:07:10
109 mon d yyyy hh:mm:ss:mili Jan 8 2014 12:07:10:690PM
110 MM-dd-yyyy 01-08-2014
111 yyyy/MM/dd 2014/01/08
112 yyyyMMdd 20140108
113 dd mon yyyy hh:mm:ss:mili 08 Jan 2014 12:07:10:690
114 hh:mm:ss:mili 12:07:10:690
120 yyyy-MM-dd hh:mm:ss 2014-01-08 12:07:10
121 yyyy-MM-dd hh:mm:ss.mili 2014-01-08 12:07:10.690
126 yyyy-MM-ddThh:mm:ss.mili 2014-01-08T12:07:10.690
127 yyyy-dd-MMThh:mm:ss.mili 2014-01-08T12:07:10.690
130 I have no idea what format this is 7 ???? ????? 1435 12:07:10:69
131 I also can't figure this one out 7/03/1435 12:07:10:690PM