CONVERT 函数的用法

来源:互联网 发布:php http build query 编辑:程序博客网 时间:2024/05/16 16:16

CONVERT 函数 [数据类型转换]

--------------------------------------------------------------------------------

功能 
返回转换成提供的数据类型的表达式。

语法 
CONVERT ( data type, expression [ , format-style ] )

参数 
data type    表达式将转换成的数据类型。

expression    要转换的表达式。

format
-style    对于将字符串转换为日期或时间数据类型以及相反的转换过程,format-style 是描述要使用的日期格式字符串的样式代码。 format-style 参数的值具有下列含义:不含世纪 (yy)  含世纪 (yyyy)  输出  
-  0 或 100  Mmm dd yyyy hh:nn:ss:sss AM(或 PM)  
1  101  mm/dd/yy[yy]  
2  102  [yy]yy.mm.dd  
3  103  dd/mm/yy[yy]  
4  104  dd.mm.yy[yy]  
5  105  dd-mm-yy[yy]  
6  106  dd Mmm yy[yy]  
7  107  Mmm dd, yy[yy]  
8  108  hh:nn:ss  
-  9 或 109  Mmm dd yyyy hh:nn:ss:sssAM(或 PM)  
10  110  mm-dd-yy[yy]  
11  111  [yy]yy/mm/dd  
12  112  [yy]yymmdd  
13  113  dd Mmm yyy hh:nn:ss:sss(24 小时制,欧洲缺省时间 + 毫秒,4 位数年份)  
14  114  hh:nn:ss:sss(24 小时制)  
20  120  yyyy-mm-dd hh:nn:ss:sss(24 小时制,ODBC 规范,4 位数年份)  
21  121  yyyy-mm-dd hh:nn:ss.sss(24 小时制,ODBC 规范加毫秒,4 位数年份)  

 

使用 CONVERT

CONVERT (data_type[(length)], expression [, style])


select CONVERT(varchargetdate(), 120 ) 
2004-09-12 11:06:08 

select replace(replace(replace(CONVERT(varchargetdate(), 120 ),/'-/',/'/'),/' /',/'/'),/':/',/'/'
20040912110608 

select CONVERT(varchar(12) , getdate(), 111 ) 
2004/09/12 

select CONVERT(varchar(12) , getdate(), 112 ) 
20040912 

select CONVERT(varchar(12) , getdate(), 102 ) 
2004.09.12 

select CONVERT(varchar(12) , getdate(), 101 ) 
09/12/2004 

select CONVERT(varchar(12) , getdate(), 103 ) 
12/09/2004 

select CONVERT(varchar(12) , getdate(), 104 ) 
12.09.2004 

select CONVERT(varchar(12) , getdate(), 105 ) 
12-09-2004 

select CONVERT(varchar(12) , getdate(), 106 ) 
12 09 2004 

select CONVERT(varchar(12) , getdate(), 107 ) 
09 122004 

select CONVERT(varchar(12) , getdate(), 108 ) 
11:06:08 

select CONVERT(varchar(12) , getdate(), 109 ) 
09 12 2004 1 

select CONVERT(varchar(12) , getdate(), 110 ) 
09-12-2004 

select CONVERT(varchar(12) , getdate(), 113 ) 
12 09 2004 1 

select CONVERT(varchar(12) , getdate(), 114 ) 
11:06:08.177

原创粉丝点击