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

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

具体的看代码及效果吧!

-- =============================================
-- Author:        <Author,Jearay>
-- Create date: <Create Date,,>
-- Description:    <Description,返回连续日期(年份或月份或日期)>
-- =============================================
CREATE FUNCTION [dbo].[fn_GetContinuousDate]
(
    @date datetime, --基准日期
    @type nvarchar(10),--'year','month','day'
    @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
    RETURN 
END



GO
View Code

函数调用示例:

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

结果如下:

猜你喜欢

转载自www.cnblogs.com/jearay/p/9296108.html