Friday, May 13, 2016

MSSQL - How to convert DateTime to VarChar.

The below query convert DateTime to VarChar.
If you execute it, you can understand easily.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
SELECT 10, 'mm-dd-yy', CONVERT(VARCHAR, GETDATE(), 10)
UNION
SELECT 11, 'yy/mm/dd', CONVERT(VARCHAR, GETDATE(), 11)
UNION
SELECT 12, 'yymmdd', CONVERT(VARCHAR, GETDATE(), 12)
UNION
SELECT 23, 'yyyy-mm-dd', CONVERT(VARCHAR, GETDATE(), 23)
UNION
SELECT 100, 'mon dd yyyy hh:miAM(PM)', CONVERT(VARCHAR, GETDATE(), 100)
UNION
SELECT 101, 'mm/dd/yyyy', CONVERT(VARCHAR, GETDATE(), 101)
UNION
SELECT 102, 'yyyy.mm.dd', CONVERT(VARCHAR, GETDATE(), 102)
UNION
SELECT 103, 'dd/mm/yyyy', CONVERT(VARCHAR, GETDATE(), 103)
UNION
SELECT 104, 'dd.mm.yyyy', CONVERT(VARCHAR, GETDATE(), 104)
UNION
SELECT 105, 'dd-mm-yyyy', CONVERT(VARCHAR, GETDATE(), 105)
UNION
SELECT 106, 'dd mon yyyy', CONVERT(VARCHAR, GETDATE(), 106)
UNION
SELECT 107, 'Mon dd, yyyy', CONVERT(VARCHAR, GETDATE(), 107)
UNION
SELECT 108, 'hh:mm:ss', CONVERT(VARCHAR, GETDATE(), 108)
UNION
SELECT 109, 'mon dd yyyy hh:mi:ss:mmmAM(PM)', CONVERT(VARCHAR, GETDATE(), 109)
UNION
SELECT 110, 'mm-dd-yyyy', CONVERT(VARCHAR, GETDATE(), 110)
UNION
SELECT 111, 'yyyy/mm/dd', CONVERT(VARCHAR, GETDATE(), 111)
UNION
SELECT 112, 'yyyymmdd', CONVERT(VARCHAR, GETDATE(), 112)
UNION
SELECT 113, 'mon yyyy hh:mi:ss:mmm (24h)', CONVERT(VARCHAR, GETDATE(), 113)
UNION
SELECT 114, 'hh:mi:ss:mmm (24h)', CONVERT(VARCHAR, GETDATE(), 114)
UNION
SELECT 120, 'yyyy-mm-dd hh:mi:ss (24h)', CONVERT(VARCHAR, GETDATE(), 120)
UNION
SELECT 121, 'yyyy-mm-dd hh:mi:ss.mmm (24h)', CONVERT(VARCHAR, GETDATE(), 121)
UNION
SELECT 126, 'yyyy-mm-ddThh:mi:ss.mmm', CONVERT(VARCHAR, GETDATE(), 126)

No comments:

Post a Comment