一個SQL Server中的FormatDatetime函式
1 Create function FormatDateTime(@Date datetime,@formatStr varchar(20))
2 returns varchar(16)
3 as
4 begin
5 declare @tempstr varchar(20),@index int,@retStr varchar(20),@formatLen int,@str1 varchar(6),@str2 varchar(6),@str3 varchar(6),@j int
6 declare @tempformat varchar(20)
7 select @tempformat=@formatStr,@formatStr = Upper(@formatStr),@index=-1,@retstr=''
8 if @formatStr='MM/DD/YYYY'
9 set @retstr= convert(varchar(10),@date,101)
10 else if @formatstr='YYYY-MM-DD'
11 set @retstr = Convert(char(10),@Date,20)
12 else if @formatStr='YYYY.MM.DD'
13 set @retstr= Convert(varchar(10),@Date,102)
14 else if @formatStr='YYYY/MM/DD'
15 set @retstr= Convert(varchar(10),@Date,111)
16 else if @formatStr='DD/MM/YYYY'
17 set @retstr= Convert(varchar(10),@Date,103)
18 else if @formatStr='DD.MM.YYYY'
19 set @retstr= Convert(varchar(10),@Date,104)
20 else if @formatStr='DD-MM-YYYY'
21 set @retstr= Convert(varchar(10),@Date,105)
22 else if @formatStr='YYYYMMDD'
23 set @retstr= Convert(varchar(10),@Date,112)
24 else
25 begin
26 select @tempformat=@formatStr,@formatLen = len(@formatStr)
27 if @formatLen>8
28 begin
29 set @index=charindex('M',@tempformat)
30 select @str1=right(left(@tempformat,@index-1),@index-5),@str2=right(@tempformat,@formatLen-@index-1)
31 select @index=charindex('D',@str2),@str3=@str2
32 set @str2=left(@str2,@index-1)
33 set @str3=right(@str3,len(@str3)-@index-1)
34 end
35 select @tempstr = Convert(char(10),@Date,20),@str1=isnull(@str1,''),@str2=isnull(@str2,''),@str3=isnull(@str3,''),@j=0
36 while @index <> 0
37 begin
38 set @index = charindex('-',@tempstr)
39 if @j=0
40 select @retstr=left(@tempstr,@index-1)+@str1,@j=@j+1
41 else set @retstr=@retstr+left(@tempstr,@index-1)+@str2
42 select @tempstr=right(@tempstr,len(@tempstr)-@index)
43 set @index= charindex('-',@tempstr)
44 end
45 set @retstr=@retstr+@tempstr+@str3
46 end
47 return @retstr
48 end
2 returns varchar(16)
3 as
4 begin
5 declare @tempstr varchar(20),@index int,@retStr varchar(20),@formatLen int,@str1 varchar(6),@str2 varchar(6),@str3 varchar(6),@j int
6 declare @tempformat varchar(20)
7 select @tempformat=@formatStr,@formatStr = Upper(@formatStr),@index=-1,@retstr=''
8 if @formatStr='MM/DD/YYYY'
9 set @retstr= convert(varchar(10),@date,101)
10 else if @formatstr='YYYY-MM-DD'
11 set @retstr = Convert(char(10),@Date,20)
12 else if @formatStr='YYYY.MM.DD'
13 set @retstr= Convert(varchar(10),@Date,102)
14 else if @formatStr='YYYY/MM/DD'
15 set @retstr= Convert(varchar(10),@Date,111)
16 else if @formatStr='DD/MM/YYYY'
17 set @retstr= Convert(varchar(10),@Date,103)
18 else if @formatStr='DD.MM.YYYY'
19 set @retstr= Convert(varchar(10),@Date,104)
20 else if @formatStr='DD-MM-YYYY'
21 set @retstr= Convert(varchar(10),@Date,105)
22 else if @formatStr='YYYYMMDD'
23 set @retstr= Convert(varchar(10),@Date,112)
24 else
25 begin
26 select @tempformat=@formatStr,@formatLen = len(@formatStr)
27 if @formatLen>8
28 begin
29 set @index=charindex('M',@tempformat)
30 select @str1=right(left(@tempformat,@index-1),@index-5),@str2=right(@tempformat,@formatLen-@index-1)
31 select @index=charindex('D',@str2),@str3=@str2
32 set @str2=left(@str2,@index-1)
33 set @str3=right(@str3,len(@str3)-@index-1)
34 end
35 select @tempstr = Convert(char(10),@Date,20),@str1=isnull(@str1,''),@str2=isnull(@str2,''),@str3=isnull(@str3,''),@j=0
36 while @index <> 0
37 begin
38 set @index = charindex('-',@tempstr)
39 if @j=0
40 select @retstr=left(@tempstr,@index-1)+@str1,@j=@j+1
41 else set @retstr=@retstr+left(@tempstr,@index-1)+@str2
42 select @tempstr=right(@tempstr,len(@tempstr)-@index)
43 set @index= charindex('-',@tempstr)
44 end
45 set @retstr=@retstr+@tempstr+@str3
46 end
47 return @retstr
48 end
用法
select dbo. FormatDatetime(GetDate(),'YYYY年MM月DD日')
@formatStr格式串支援:
MM/DD/YYYY
YYYY-MM-DD
YYYY.MM.DD
YYYY/MM/DD
DD/MM/YYYY
DD.MM.YYYY
DD-MM-YYYY
YYYYMMDD或者
類似YYYY年MM月DD日
YYYY MM之間最多支援兩個漢字,MM DD之間也最多支援兩個個漢字
select dbo. FormatDatetime(GetDate(),'YYYY元年MM月份DD日')
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-624190/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL Server 2016 函式:CASTSQLServer函式AST
- SQL Server常用函式整理SQLServer函式
- SQL Server中row_number函式的常見用法SQLServer函式
- Sql 中的 left 函式、right 函式SQL函式
- SQL server儲存過程函式SQLServer儲存過程函式
- 【SQL Server】常見系統函式SQLServer函式
- js中在一個函式中引用另一個函式中的函式,可以這麼做JS函式
- SQL中的cast()函式SQLAST函式
- SQL Server資料庫中Substring函式的用法例項詳解SQLServer資料庫函式
- vue在一個函式中呼叫另外一個函式Vue函式
- sql server 資料型別轉換函式SQLServer資料型別函式
- sql server中的一個坑-len與datalength區別SQLServer
- SQL Server 中的一些概念SQLServer
- SQL Server建立使用者函式與應用SQLServer函式
- SQL Server最佳化標量函式改寫內聯表值函式SQLServer函式
- SQL SERVER資料庫datediff函式引發的效能問題SQLServer資料庫函式
- SQL Server資料庫————模糊查詢和聚合函式SQLServer資料庫函式
- SQL中的替換函式replace()使用SQL函式
- Sql中SYSDATE函式的使用方法SQL函式
- SQL SERVER 日期和時間資料型別及函式 (Transact-SQL)SQLServer資料型別函式
- 【SQL】19 SQL函式SQL函式
- SQL 獲取SQL Server中兩個日期之間的所有日期SQLServer
- sql函式SQL函式
- SQL-函式 - 聚合函式SQL函式
- SQL中的常用的字串處理函式大全SQL字串函式
- SQL Server 2008中Analysis Services的新特性——深入SQL Server 2008SQLServer
- Sql Server資料庫開窗函式Over()的使用例項詳解SQLServer資料庫函式
- Serverless 解惑——函式計算如何訪問 SQL Server 資料庫Server函式SQL資料庫
- sql中select列有自定義函式 dblinkSQL函式
- SQL Server 中的 ACID 屬性SQLServer
- Python中將函式作為另一個函式的引數傳入並呼叫Python函式
- T-SQL——函式——字串操作函式SQL函式字串
- 分析WordPress中esc_sql函式引起的注入危害SQL函式
- SQL 視窗函式SQL函式
- SQL LEN()函式用法SQL函式
- T-SQL——函式——時間操作函式SQL函式
- 一個函式學excel函式Excel
- SQL Server中的版本號如何理解SQLServer
- sql常用函式詳解(一)——字串擷取SQL函式字串