• sql server获取连续年份、月份、日


    if exists(select 1 from sysobjects where type='TF' and name='ATPublic_TF_GetSummaryDate')
        drop function ATPublic_TF_GetSummaryDate
    go
    create function ATPublic_TF_GetSummaryDate
    (
        @calltype varchar(1)='',    --0:年 1:月    1:日
        @startdate DATE,            --开始日期
        @enddate DATE                --结束日期
    ) 
     /*** 
        Author: 
        Create date: 2018-01-18
        Description: 循环返回日期   
        Example: 
    
    ***/
    RETURNS @retDateValue TABLE (datevalue varchar(32))                                                                               
    as    
    begin
        if @calltype='0'
        begin
            insert into @retDateValue(datevalue)
            select substring(convert(varchar(32),DATEADD(YEAR,number,@startdate),21),1,4) as yearvalue                                                                              
            from master..spt_values WHERE TYPE = 'P' AND DATEADD(YEAR,number,@startdate) <= @enddate                
        end
        else if @calltype='1'
        begin
            insert into @retDateValue(datevalue)
            select substring(convert(varchar(32),DATEADD(MONTH,number,@startdate),21),1,7) as yearvalue                                                                              
            from master..spt_values WHERE TYPE = 'P' AND DATEADD(MONTH,number,@startdate) < @enddate                
        end
        else if @calltype='2'
        begin
            insert into @retDateValue(datevalue)
            select convert(varchar(32),DATEADD(DAY,number,@startdate),21)+' 00:00:00' as yearvalue                                                                              
            from master..spt_values WHERE TYPE = 'P' AND DATEADD(DAY,number,@startdate) < @enddate                
        end
        return
    end
                                                                            
    go

    调用示例:

    declare @beginDate varchar(32)='', @endDate varchar(32)='',@datebefore datetime
    select @beginDate='2017-01-01',@endDate='2017-05-31'
    select @datebefore=CONVERT(datetime,@beginDate)
    select datevalue   from ATPublic_TF_GetSummaryDate('0',@beginDate,@endDate)
    select datevalue from ATPublic_TF_GetSummaryDate('1',@beginDate,dateadd(YEAR,1,@datebefore))
    select datevalue from ATPublic_TF_GetSummaryDate('1',@beginDate,dateadd(MONTH,1,@datebefore))
    select datevalue from ATPublic_TF_GetSummaryDate('2',@beginDate,dateadd(MONTH,1,@datebefore))
  • 相关阅读:
    Cache与主存的映射方式 例题
    软件测试实验三
    指令的寻址方式
    指令系统
    Cache与主存的映射方式
    指令周期与数据通路
    SQL语句中Cast的用法
    详解SQL Server STUFF的用法
    使用adb卸载系统APP
    第四章:(1)本地运行模式
  • 原文地址:https://www.cnblogs.com/970119449blog/p/8335197.html
Copyright © 2020-2023  润新知