此隨筆主在分享平常可能用到的sql函數,用於生成連續日期(年份、月份、日期)sql
具體的看代碼及效果吧!ide
-- ============================================= -- Author: <Author,Jearay> -- Create date: <Create Date,2018/7/12> -- Description: <Description,返回連續日期(年份或月份或日期)> -- ============================================= CREATE FUNCTION [dbo].[fn_GetContinuousDate] ( @date datetime, --基準日期 @type nvarchar(10),--'year、y','month、mon、m','day、d','yearmonth、ym','monthday、md' @prev int, --往前數量 @next int --後續數量 ) RETURNS @return TABLE ( DataDate date,DateAlis nvarchar(20),DateCommon nvarchar(20) ) AS BEGIN declare @tempDate date,@tempDateAlis nvarchar(20),@tempDateCommon nvarchar(20),@index int=1 --年份 if LOWER(@type)=N'year' or LOWER(@type)=N'y' begin set @date=dateadd(year,DATEDIFF(year,0,@date),0) --寫入往前數量的年份 while @prev>0 begin set @tempDate=dateadd(year,-@prev,@date) insert @return select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年',cast(year(@tempDate) as nvarchar(4)) set @prev=@prev-1 end --寫入當年 insert @return select @date,cast(year(@date) as nvarchar(4))+N'年',cast(year(@date) as nvarchar(4)) --寫入後續數量的年份 while @next-@index>=0 begin set @tempDate=dateadd(year,@index,@date) insert @return select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年',cast(year(@tempDate) as nvarchar(4)) set @index=@index+1 end end --月份 else if LOWER(@type)=N'month' or LOWER(@type)=N'm' or LOWER(@type)=N'mon' begin set @date=dateadd(month,DATEDIFF(month,0,@date),0) --寫入往前數量的月份 while @prev>0 begin set @tempDate=dateadd(month,-@prev,@date) insert @return 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)) set @prev=@prev-1 end --寫入當月 insert @return 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)) --寫入後續數量的月份 while @next-@index>=0 begin set @tempDate=dateadd(month,@index,@date) insert @return 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)) set @index=@index+1 end end --日期 else if LOWER(@type)=N'day' or LOWER(@type)=N'd' begin set @date=dateadd(day,DATEDIFF(day,0,@date),0) --寫入往前數量的日期 while @prev>0 begin set @tempDate=dateadd(day,-@prev,@date) insert @return select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日' ,cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2))+N'/'+cast(day(@tempDate) as nvarchar(2)) set @prev=@prev-1 end --寫入當日 insert @return select @date,cast(year(@date) as nvarchar(4))+N'年'+cast(month(@date) as nvarchar(2))+N'月'+cast(day(@date) as nvarchar(2))+N'日' ,cast(year(@date) as nvarchar(4))+N'/'+cast(month(@date) as nvarchar(2))+N'/'+cast(day(@date) as nvarchar(2)) --寫入後續數量的日期 while @next-@index>=0 begin set @tempDate=dateadd(day,@index,@date) insert @return select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日' ,cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2))+N'/'+cast(day(@tempDate) as nvarchar(2)) set @index=@index+1 end end --年中月 else if LOWER(@type)=N'yearmonth' or LOWER(@type)=N'ym' begin set @date=dateadd(year,DATEDIFF(year,0,@date),0) set @index=0 --寫入年對應月份 while 12-@index>0 begin set @tempDate=dateadd(month,@index,@date) insert @return select @tempDate,cast(month(@tempDate) as nvarchar(2))+N'月' ,cast(year(@tempDate) as nvarchar(4))+N'/'+cast(month(@tempDate) as nvarchar(2)) set @index=@index+1 end end --月中日, 分天然月和指定月 else if LOWER(@type)=N'monthday' or LOWER(@type)=N'md' begin --指定月 --指定月開始日期、結束日期 if @prev>0 and @next>0 begin declare @endDate date set @date=dateadd(month,DATEDIFF(month,0,@date),0) --獲取月份 set @endDate=dateadd(day,@next,@date) set @index=datediff(day,@endDate,dateadd(day,@prev-1,dateadd(month,-1,@date))) --寫入月對應日期 while @index<0 begin set @tempDate=dateadd(day,@index,@endDate) insert @return select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日' ,@tempDate set @index=@index+1 end end --天然月 else begin set @date=dateadd(month,DATEDIFF(month,0,@date),0) set @index=datediff(day,dateadd(month,1,@date),@date) set @date=dateadd(month,1,@date) --寫入月對應日期 while @index<0 begin set @tempDate=dateadd(day,@index,@date) insert @return select @tempDate,cast(year(@tempDate) as nvarchar(4))+N'年'+cast(month(@tempDate) as nvarchar(2))+N'月'+cast(day(@tempDate) as nvarchar(2))+N'日' ,@tempDate set @index=@index+1 end end end RETURN END
函數調用示例:函數
--返回今天往前3天至今天日後2天的連續日期 select * from dbo.fn_GetContinuousDate(getdate(),'d',3,2)
結果以下:spa