sqlserver CONVERT()函数用法小结
程序员文章站
2023-12-13 15:50:58
convert的使用方法: 格式: convert(data_type,expression[,style]) 说明: 此样式一般在时间类型(datetime,smalld...
convert的使用方法:
格式:
convert(data_type,expression[,style])
说明:
此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,char,varchar)
相互转换的时候才用到.
例子:
select convert(varchar(30),getdate(),101) now
结果为
now
---------------------------------------
09/15/2001
style数字在转换时间时的含义如下
-------------------------------------------------------------------------------------------------
style(2位表示年份) | style(4位表示年份) | 输入输出格式
-------------------------------------------------------------------------------------------------
- | 0 or 100 | mon dd yyyy hh:miam(或pm)
-------------------------------------------------------------------------------------------------
1 | 101 | mm/dd/yy
-------------------------------------------------------------------------------------------------
2 | 102 | yy-mm-dd
-------------------------------------------------------------------------------------------------
3 | 103 | dd/mm/yy
-------------------------------------------------------------------------------------------------
4 | 104 | dd-mm-yy
-------------------------------------------------------------------------------------------------
5 | 105 | dd-mm-yy
-------------------------------------------------------------------------------------------------
6 | 106 | dd mon yy
-------------------------------------------------------------------------------------------------
7 | 107 | mon dd,yy
-------------------------------------------------------------------------------------------------
8 | 108 | hh:mm:ss
-------------------------------------------------------------------------------------------------
- | 9 or 109 | mon dd yyyy hh:mi:ss:mmmmam(或pm)
-------------------------------------------------------------------------------------------------
10 | 110 | mm-dd-yy
-------------------------------------------------------------------------------------------------
11 | 111 | yy/mm/dd
-------------------------------------------------------------------------------------------------
12 | 112 | yymmdd
-------------------------------------------------------------------------------------------------
- | 13 or 113 | dd mon yyyy hh:mi:ss:mmm(24小时制)
-------------------------------------------------------------------------------------------------
14 | 114 | hh:mi:ss:mmm(24小时制)
-------------------------------------------------------------------------------------------------
- | 20 or 120 | yyyy-mm-dd hh:mi:ss(24小时制)
-------------------------------------------------------------------------------------------------
- | 21 or 121 | yyyy-mm-dd hh:mi:ss:mmm(24小时制)
-------------------------------------------------------------------------------------------------
可以使用的 style 值:
sqlserver之convert 函数应用
select convert(varchar(100), getdate(), 0): 05 16 2006 10:57am
select convert(varchar(100), getdate(), 1): 05/16/06
select convert(varchar(100), getdate(), 2): 06.05.16
select convert(varchar(100), getdate(), 3): 16/05/06
select convert(varchar(100), getdate(), 4): 16.05.06
select convert(varchar(100), getdate(), 5): 16-05-06
select convert(varchar(100), getdate(), 6): 16 05 06
select convert(varchar(100), getdate(), 7): 05 16, 06
select convert(varchar(100), getdate(), 8): 10:57:46
select convert(varchar(100), getdate(), 9): 05 16 2006 10:57:46:827am
select convert(varchar(100), getdate(), 10): 05-16-06
select convert(varchar(100), getdate(), 11): 06/05/16
select convert(varchar(100), getdate(), 12): 060516
select convert(varchar(100), getdate(), 13): 16 05 2006 10:57:46:937
select convert(varchar(100), getdate(), 14): 10:57:46:967
select convert(varchar(100), getdate(), 20): 2006-05-16 10:57:47
select convert(varchar(100), getdate(), 21): 2006-05-16 10:57:47.157
select convert(varchar(100), getdate(), 22): 05/16/06 10:57:47 am
select convert(varchar(100), getdate(), 23): 2006-05-16
select convert(varchar(100), getdate(), 24): 10:57:47
select convert(varchar(100), getdate(), 25): 2006-05-16 10:57:47.250
select convert(varchar(100), getdate(), 100): 05 16 2006 10:57am
select convert(varchar(100), getdate(), 101): 05/16/2006
select convert(varchar(100), getdate(), 102): 2006.05.16
select convert(varchar(100), getdate(), 103): 16/05/2006
select convert(varchar(100), getdate(), 104): 16.05.2006
select convert(varchar(100), getdate(), 105): 16-05-2006
select convert(varchar(100), getdate(), 106): 16 05 2006
select convert(varchar(100), getdate(), 107): 05 16, 2006
select convert(varchar(100), getdate(), 108): 10:57:49
select convert(varchar(100), getdate(), 109): 05 16 2006 10:57:49:437am
select convert(varchar(100), getdate(), 110): 05-16-2006
select convert(varchar(100), getdate(), 111): 2006/05/16
select convert(varchar(100), getdate(), 112): 20060516
select convert(varchar(100), getdate(), 113): 16 05 2006 10:57:49:513
select convert(varchar(100), getdate(), 114): 10:57:49:547
select convert(varchar(100), getdate(), 120): 2006-05-16 10:57:49
select convert(varchar(100), getdate(), 121): 2006-05-16 10:57:49.700
select convert(varchar(100), getdate(), 126): 2006-05-16t10:57:49.827
select convert(varchar(100), getdate(), 130): 18 ???? ?????? 1427 10:57:49:907am
select convert(varchar(100), getdate(), 131): 18/04/1427 10:57:49:920am
格式:
convert(data_type,expression[,style])
说明:
此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,char,varchar)
相互转换的时候才用到.
例子:
select convert(varchar(30),getdate(),101) now
结果为
now
---------------------------------------
09/15/2001
style数字在转换时间时的含义如下
-------------------------------------------------------------------------------------------------
style(2位表示年份) | style(4位表示年份) | 输入输出格式
-------------------------------------------------------------------------------------------------
- | 0 or 100 | mon dd yyyy hh:miam(或pm)
-------------------------------------------------------------------------------------------------
1 | 101 | mm/dd/yy
-------------------------------------------------------------------------------------------------
2 | 102 | yy-mm-dd
-------------------------------------------------------------------------------------------------
3 | 103 | dd/mm/yy
-------------------------------------------------------------------------------------------------
4 | 104 | dd-mm-yy
-------------------------------------------------------------------------------------------------
5 | 105 | dd-mm-yy
-------------------------------------------------------------------------------------------------
6 | 106 | dd mon yy
-------------------------------------------------------------------------------------------------
7 | 107 | mon dd,yy
-------------------------------------------------------------------------------------------------
8 | 108 | hh:mm:ss
-------------------------------------------------------------------------------------------------
- | 9 or 109 | mon dd yyyy hh:mi:ss:mmmmam(或pm)
-------------------------------------------------------------------------------------------------
10 | 110 | mm-dd-yy
-------------------------------------------------------------------------------------------------
11 | 111 | yy/mm/dd
-------------------------------------------------------------------------------------------------
12 | 112 | yymmdd
-------------------------------------------------------------------------------------------------
- | 13 or 113 | dd mon yyyy hh:mi:ss:mmm(24小时制)
-------------------------------------------------------------------------------------------------
14 | 114 | hh:mi:ss:mmm(24小时制)
-------------------------------------------------------------------------------------------------
- | 20 or 120 | yyyy-mm-dd hh:mi:ss(24小时制)
-------------------------------------------------------------------------------------------------
- | 21 or 121 | yyyy-mm-dd hh:mi:ss:mmm(24小时制)
-------------------------------------------------------------------------------------------------
可以使用的 style 值:
style id | style 格式 |
---|---|
100 或者 0 | mon dd yyyy hh:miam (或者 pm) |
101 | mm/dd/yy |
102 | yy.mm.dd |
103 | dd/mm/yy |
104 | dd.mm.yy |
105 | dd-mm-yy |
106 | dd mon yy |
107 | mon dd, yy |
108 | hh:mm:ss |
109 或者 9 | mon dd yyyy hh:mi:ss:mmmam(或者 pm) |
110 | mm-dd-yy |
111 | yy/mm/dd |
112 | yymmdd |
113 或者 13 | dd mon yyyy hh:mm:ss:mmm(24h) |
114 | hh:mi:ss:mmm(24h) |
120 或者 20 | yyyy-mm-dd hh:mi:ss(24h) |
121 或者 21 | yyyy-mm-dd hh:mi:ss.mmm(24h) |
126 | yyyy-mm-ddthh:mm:ss.mmm(没有空格) |
130 | dd mon yyyy hh:mi:ss:mmmam |
131 | dd/mm/yy hh:mi:ss:mmmam |
select convert(varchar(100), getdate(), 0): 05 16 2006 10:57am
select convert(varchar(100), getdate(), 1): 05/16/06
select convert(varchar(100), getdate(), 2): 06.05.16
select convert(varchar(100), getdate(), 3): 16/05/06
select convert(varchar(100), getdate(), 4): 16.05.06
select convert(varchar(100), getdate(), 5): 16-05-06
select convert(varchar(100), getdate(), 6): 16 05 06
select convert(varchar(100), getdate(), 7): 05 16, 06
select convert(varchar(100), getdate(), 8): 10:57:46
select convert(varchar(100), getdate(), 9): 05 16 2006 10:57:46:827am
select convert(varchar(100), getdate(), 10): 05-16-06
select convert(varchar(100), getdate(), 11): 06/05/16
select convert(varchar(100), getdate(), 12): 060516
select convert(varchar(100), getdate(), 13): 16 05 2006 10:57:46:937
select convert(varchar(100), getdate(), 14): 10:57:46:967
select convert(varchar(100), getdate(), 20): 2006-05-16 10:57:47
select convert(varchar(100), getdate(), 21): 2006-05-16 10:57:47.157
select convert(varchar(100), getdate(), 22): 05/16/06 10:57:47 am
select convert(varchar(100), getdate(), 23): 2006-05-16
select convert(varchar(100), getdate(), 24): 10:57:47
select convert(varchar(100), getdate(), 25): 2006-05-16 10:57:47.250
select convert(varchar(100), getdate(), 100): 05 16 2006 10:57am
select convert(varchar(100), getdate(), 101): 05/16/2006
select convert(varchar(100), getdate(), 102): 2006.05.16
select convert(varchar(100), getdate(), 103): 16/05/2006
select convert(varchar(100), getdate(), 104): 16.05.2006
select convert(varchar(100), getdate(), 105): 16-05-2006
select convert(varchar(100), getdate(), 106): 16 05 2006
select convert(varchar(100), getdate(), 107): 05 16, 2006
select convert(varchar(100), getdate(), 108): 10:57:49
select convert(varchar(100), getdate(), 109): 05 16 2006 10:57:49:437am
select convert(varchar(100), getdate(), 110): 05-16-2006
select convert(varchar(100), getdate(), 111): 2006/05/16
select convert(varchar(100), getdate(), 112): 20060516
select convert(varchar(100), getdate(), 113): 16 05 2006 10:57:49:513
select convert(varchar(100), getdate(), 114): 10:57:49:547
select convert(varchar(100), getdate(), 120): 2006-05-16 10:57:49
select convert(varchar(100), getdate(), 121): 2006-05-16 10:57:49.700
select convert(varchar(100), getdate(), 126): 2006-05-16t10:57:49.827
select convert(varchar(100), getdate(), 130): 18 ???? ?????? 1427 10:57:49:907am
select convert(varchar(100), getdate(), 131): 18/04/1427 10:57:49:920am