字符串转日期语句

sql server中文版的默认的日期字段datetime格式是yyyy-mm-dd
thh:mm:ss.mmm例如:select getdate()2004-09-12 11:06:08.177整理了一下sql
server里面可能经常会用到的日期格式转换方法:举例如下:select
convert(varchar, getdate(), 120 )2004-09-12 11:06:08select
convert(varchar(100), getdate(), 0): 05 16 2006 10:57amselect
convert(varchar(100), getdate(), 1): 05/16/06select
convert(varchar(100), getdate(), 2): 06.05.16select
convert(varchar(100), getdate(), 3): 16/05/06select
convert(varchar(100), getdate(), 4): 16.05.06select
convert(varchar(100), getdate(), 5): 16-05-06select
convert(varchar(100), getdate(), 6): 16 05 06select
convert(varchar(100), getdate(), 7): 05 16, 06select
convert(varchar(100), getdate(), 8): 10:57:46select
convert(varchar(100), getdate(), 9): 05 16 2006 10:57:46:827amselect
convert(varchar(100), getdate(), 10): 05-16-06select
convert(varchar(100), getdate(), 11): 06/05/16select
convert(varchar(100), getdate(), 12): 060516select convert(varchar(100),
getdate(), 13): 16 05 2006 10:57:46:937select convert(varchar(100),
getdate(), 14): 10:57:46:967select convert(varchar(100), getdate(), 20):
2006-05-16 10:57:47select convert(varchar(100), getdate(), 21):
2006-05-16 10:57:47.157select convert(varchar(100), getdate(), 22):
05/16/06 10:57:47 amselect convert(varchar(100), getdate(), 23):
2006-05-16select convert(varchar(100), getdate(), 24): 10:57:47select
convert(varchar(100), getdate(), 25): 2006-05-16 10:57:47.250select
convert(varchar(100), getdate(), 100): 05 16 2006 10:57amselect
convert(varchar(100), getdate(), 101): 05/16/2006select
convert(varchar(100), getdate(), 102): 2006.05.16select
convert(varchar(100), getdate(), 103): 16/05/2006select
convert(varchar(100), getdate(), 104): 16.05.2006select
convert(varchar(100), getdate(), 105): 16-05-2006select
convert(varchar(100), getdate(), 106): 16 05 2006select
convert(varchar(100), getdate(), 107): 05 16, 2006select
convert(varchar(100), getdate(), 108): 10:57:49select
convert(varchar(100), getdate(), 109): 05 16 2006 10:57:49:437amselect
convert(varchar(100), getdate(), 110): 05-16-2006select
convert(varchar(100), getdate(), 111): 2006/05/16select
convert(varchar(100), getdate(), 112): 20060516select
convert(varchar(100), getdate(), 113): 16 05 2006 10:57:49:513select
convert(varchar(100), getdate(), 114): 10:57:49:547select
convert(varchar(100), getdate(), 120): 2006-05-16 10:57:49select
convert(varchar(100), getdate(), 121): 2006-05-16 10:57:49.700select
convert(varchar(100), getdate(), 126): 2006-05-16t10:57:49.827select
convert(varchar(100), getdate(), 130): 18 ???? ?????? 1427
10:57:49:907amselect convert(varchar(100), getdate(), 131): 18/04/1427
10:57:49:920am

方法二

在c#中用惯了datetime.tostring(yyyy-mm-dd),datetime.tostring(mm/dd/yyyy)这种日期与字符串的转换方式,在sql
server中没得用,于是乎写了个function,功能跟.net
中的datetime.tostring(formatprovide)方法差不多,不过只实现了日期部分,有兴趣的朋友可以把时间部分补充出来。

create function fn_datetostring(@date datetime, @format varchar(20))

returns varchar(20)

as

begin

declare @result varchar(20)

select @result =
(replace(replace(replace(@format,’yyyy’,’20’+substring(convert(char(8),@date,3),7,2)),’mm’,substring(convert(char(8),@date,3),4,2)),’dd’,substring(convert(char(8),@date,3),1,2)))

return @result

end

使用:

select dbo.fn_datetostring(getdate(),’yyyy-mm-dd’)

Leave a Comment.