SQL Convert的用法及获取时间,以及用substring获取相应的日期,小时,分钟,convertsubstring
分享于 点击 46609 次 点评:185
SQL Convert的用法及获取时间,以及用substring获取相应的日期,小时,分钟,convertsubstring
语法
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)
使用 CONVERT:
CONVERT (data_type[(length)], expression [, style])
select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08
select replace(replace(replace(CONVERT(varchar, getdate(), 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 12, 2004
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
下面是Substring截取
select substring(convert(varchar,convert(datetime,getdate(),120),120),12,5)
得到的是当前时间:18:08
如果想获取日期,改后面(12,5)相应的值
当然,还有另一种方法,如
SELECT DATEPART(hh,GETDATE()) //当前小时
SELECT DATEPART(DD,GETDATE()) //当前分钟
相关文章
- 暂无相关文章
用户点评