sql生成連續日期(年份、月份、日期)
阿新 • • 發佈:2022-11-30
常可能用到的sql函式,用於生成連續日期(年份、月份、日期),具體的看程式碼及效果吧!
1 -- ============================================= 2 -- Author: <Author,Jearay> 3 -- Create date: <Create Date,2018/7/12> 4 -- Description: <Description,返回連續日期(年份或月份或日期)> 5 -- ============================================= 6CREATE FUNCTION [dbo].[fn_GetContinuousDate] 7 ( 8 @date datetime, --基準日期 9 @type nvarchar(10),--'year、y','month、mon、m','day、d','yearmonth、ym','monthday、md' 10 @prev int, --往前數量 11 @next int --後續數量 12 ) 13 RETURNS 14 @return TABLE 15 ( 16 DataDate date,DateAlis nvarchar(20),DateCommon nvarchar(20) 17 ) 18 AS 19 BEGIN 20 declare @tempDate date,@tempDateAlis nvarchar(20),@tempDateCommon nvarchar(20),@index int=1 21 --年份 22 if LOWER(@type)=N'year' or LOWER(@type)=N'y' 23 begin 24 set @date=dateadd(year,DATEDIFF(year,0,@date),0) 25 --寫入往前數量的年份26 while @prev>0 27 begin 28 set @tempDate=dateadd(year,-@prev,@date) 29 insert @return 30 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年',cast(year(@tempDate) as nvarchar(4)) 31 set @prev=@prev-1 32 end 33 --寫入當年 34 insert @return 35 select @date,cast(year(@date) as nvarchar(4))+N'年',cast(year(@date) as nvarchar(4)) 36 --寫入後續數量的年份 37 while @next-@index>=0 38 begin 39 set @tempDate=dateadd(year,@index,@date) 40 insert @return 41 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年',cast(year(@tempDate) as nvarchar(4)) 42 set @index=@index+1 43 end 44 45 end 46 --月份 47 else if LOWER(@type)=N'month' or LOWER(@type)=N'm' or LOWER(@type)=N'mon' 48 begin 49 set @date=dateadd(month,DATEDIFF(month,0,@date),0) 50 --寫入往前數量的月份 51 while @prev>0 52 begin 53 set @tempDate=dateadd(month,-@prev,@date) 54 insert @return 55 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月',cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2)) 56 set @prev=@prev-1 57 end 58 --寫入當月 59 insert @return 60 select @date,cast(year(@date) as nvarchar(4))+N'年'+cast(month(@date) as nvarchar(2))+N'月',cast(year(@date) as nvarchar(4))+N'/'+cast(month(@date) as nvarchar(2)) 61 --寫入後續數量的月份 62 while @next-@index>=0 63 begin 64 set @tempDate=dateadd(month,@index,@date) 65 insert @return 66 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月',cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2)) 67 set @index=@index+1 68 end 69 70 end 71 --日期 72 else if LOWER(@type)=N'day' or LOWER(@type)=N'd' 73 begin 74 set @date=dateadd(day,DATEDIFF(day,0,@date),0) 75 --寫入往前數量的日期 76 while @prev>0 77 begin 78 set @tempDate=dateadd(day,-@prev,@date) 79 insert @return 80 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日' 81 ,cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2))+N'/'+cast(day(@tempDate) as nvarchar(2)) 82 set @prev=@prev-1 83 end 84 --寫入當日 85 insert @return 86 select @date,cast(year(@date) as nvarchar(4))+N'年'+cast(month(@date) as nvarchar(2))+N'月'+cast(day(@date) as nvarchar(2))+N'日' 87 ,cast(year(@date) as nvarchar(4))+N'/'+cast(month(@date) as nvarchar(2))+N'/'+cast(day(@date) as nvarchar(2)) 88 --寫入後續數量的日期 89 while @next-@index>=0 90 begin 91 set @tempDate=dateadd(day,@index,@date) 92 insert @return 93 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日' 94 ,cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2))+N'/'+cast(day(@tempDate) as nvarchar(2)) 95 set @index=@index+1 96 end 97 98 end 99 --年中月 100 else if LOWER(@type)=N'yearmonth' or LOWER(@type)=N'ym' 101 begin 102 set @date=dateadd(year,DATEDIFF(year,0,@date),0) 103 set @index=0 104 --寫入年對應月份 105 while 12-@index>0 106 begin 107 set @tempDate=dateadd(month,@index,@date) 108 insert @return 109 select @tempDate,cast(month(@tempDate) as nvarchar(2))+N'月' 110 ,cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2)) 111 set @index=@index+1 112 end 113 end 114 --月中日, 分自然月和指定月 115 else if LOWER(@type)=N'monthday' or LOWER(@type)=N'md' 116 begin 117 --指定月 118 --指定月開始日期、結束日期 119 if @prev>0 and @next>0 120 begin 121 declare @endDate date 122 set @date=dateadd(month,DATEDIFF(month,0,@date),0) --獲取月份 123 set @endDate=dateadd(day,@next,@date) 124 set @index=datediff(day,@endDate,dateadd(day,@prev-1,dateadd(month,-1,@date))) 125 --寫入月對應日期 126 while @index<0 127 begin 128 set @tempDate=dateadd(day,@index,@endDate) 129 insert @return 130 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日' 131 ,@tempDate 132 set @index=@index+1 133 end 134 end 135 --自然月 136 else 137 begin 138 set @date=dateadd(month,DATEDIFF(month,0,@date),0) 139 set @index=datediff(day,dateadd(month,1,@date),@date) 140 set @date=dateadd(month,1,@date) 141 --寫入月對應日期 142 while @index<0 143 begin 144 set @tempDate=dateadd(day,@index,@date) 145 insert @return 146 select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日' 147 ,@tempDate 148 set @index=@index+1 149 end 150 end 151 152 153 end 154 RETURN 155 END
函式呼叫示例:
--返回今天往前3天至今天往後2天的連續日期 select * from dbo.fn_GetContinuousDate(getdate(),'d',3,2)
結果如下: