sql生成连续日期(年份、月份、日期)

常可能用到的sql函数,用于生成连续日期(年份、月份、日期),具体的看代码及效果吧!

  1 -- =============================================
  2 -- Author:        <Author,Jearay>
  3 -- Create date: <Create Date,2018/7/12>
  4 -- Description:    <Description,返回连续日期(年份或月份或日期)>
  5 -- =============================================
  6 CREATE 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)

结果如下:

 

posted @ 2022-11-30 14:14  每天进步多一点  阅读(951)  评论(0编辑  收藏  举报