SQL Server數(shù)據(jù)庫中可用格式字符串干什么?
此文章主要向大家描述的是SQL Server數(shù)據(jù)庫中用格式字符串定制某些日期轉(zhuǎn)換為字符串的實(shí)際操作過程,我們習(xí)慣在C#中用慣了DateTime.ToString("yyyy-MM-dd"),DateTime.ToString("MM/dd/yyyy")這種日期與字符串的轉(zhuǎn)換方式。
在SQL server中沒得用,于是乎寫了個FUNCTION,功能跟.net 中的DateTime.ToString("formatprovide")方法差不多,不過只實(shí)現(xiàn)了日期部分,有興趣的朋友可以把時間部分補(bǔ)充出來。
- 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')
得到結(jié)果:2005-07-12
上述的相關(guān)內(nèi)容就是對SQL Server數(shù)據(jù)庫中用格式字符串定制日期轉(zhuǎn)換為字符串的描述,希望會給你帶來一些幫助在此方面。
以上的相關(guān)內(nèi)容就是對SQL Server數(shù)據(jù)庫中用格式字符串定制日期轉(zhuǎn)換為字符串的描述,望你能有所收獲。
【編輯推薦】