SQL中也可以用格式字符串定制日期转字符串

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')

posted @ 2011-08-05 09:23  qanholas  阅读(294)  评论(0编辑  收藏  举报