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

此随笔主在分享日常可能用到的sql函数,用于生成连续日期(年份、月份、日期)

具体的看代码及效果吧!

-- =============================================
-- 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
View Code

 

函数调用示例:

--返回今天往前3天至今天往后2天的连续日期
select * from dbo.fn_GetContinuousDate(getdate(),'d',3,2)

结果如下:

 

 

 

 

 

 

转载于:https://www.cnblogs.com/jearay/p/9296108.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值