Get a list of dates between two dates using a function

前端 未结 21 1074
天涯浪人
天涯浪人 2020-11-22 06:25

My question is similar to this MySQL question, but intended for SQL Server:

Is there a function or a query that will return a list of days between two dates? For exa

相关标签:
21条回答
  • 2020-11-22 06:38
    Declare @date1 date = '2016-01-01'
                  ,@date2 date = '2016-03-31'
                  ,@date_index date
    Declare @calender table (D date)
    SET @date_index = @date1
    WHILE @date_index<=@date2
    BEGIN
    INSERT INTO @calender
    SELECT @date_index
    
    SET @date_index = dateadd(day,1,@date_index)
    
    IF @date_index>@date2
    Break
    ELSE
    Continue
    END
    
    0 讨论(0)
  • 2020-11-22 06:40

    Answer is avialbe here How to list all dates between two dates

    Create Procedure SelectDates(@fromDate Date, @toDate Date)
    AS
    BEGIN
        SELECT DATEADD(DAY,number,@fromDate) [Date]
        FROM master..spt_values
        WHERE type = 'P'
        AND DATEADD(DAY,number,@fromDate) < @toDate
    
    END
    
    0 讨论(0)
  • 2020-11-22 06:41

    Perhaps if you wish to go an easier way, this should do it.

    WITH date_range (calc_date) AS (
        SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP) - 6, 0)
            UNION ALL SELECT DATEADD(DAY, 1, calc_date)
                FROM date_range
                WHERE DATEADD(DAY, 1, calc_date) < CURRENT_TIMESTAMP)
    SELECT calc_date
    FROM date_range;
    

    But the temporary table is a very good approach also. Perhaps shall you also consider a populated calendar table.

    0 讨论(0)
  • 2020-11-22 06:41

    In case you want to print years starting from a particular year till current date. Just altered the accepted answer.

    WITH mycte AS
        (
          SELECT YEAR(CONVERT(DATE, '2006-01-01',102)) DateValue
          UNION ALL
          SELECT  DateValue + 1
          FROM    mycte   
          WHERE   DateValue + 1 < = YEAR(GETDATE())
        )
        SELECT  DateValue
        FROM    mycte
    
    OPTION (MAXRECURSION 0)
    
    0 讨论(0)
  • 2020-11-22 06:42

    I'm an oracle guy, but I believe MS SQL Server has support for the connect by clause:

    select  sysdate + level
    from    dual
    connect by level <= 10 ;
    

    The output is:

    SYSDATE+LEVEL
    05-SEP-09
    06-SEP-09
    07-SEP-09
    08-SEP-09
    09-SEP-09
    10-SEP-09
    11-SEP-09
    12-SEP-09
    13-SEP-09
    14-SEP-09
    

    Dual is just a 'dummy' table that comes with oracle (it contains 1 row and the word 'dummy' as the value of the single column).

    0 讨论(0)
  • 2020-11-22 06:42

    -- ### Six of one half dozen of another. Another method assuming MsSql

    Declare @MonthStart    datetime   = convert(DateTime,'07/01/2016')
    Declare @MonthEnd      datetime   = convert(DateTime,'07/31/2016')
    Declare @DayCount_int       Int   = 0 
    Declare @WhileCount_int     Int   = 0
    
    set @DayCount_int = DATEDIFF(DAY, @MonthStart, @MonthEnd)
    select @WhileCount_int
    WHILE @WhileCount_int < @DayCount_int + 1
    BEGIN
       print convert(Varchar(24),DateAdd(day,@WhileCount_int,@MonthStart),101)
       SET @WhileCount_int = @WhileCount_int + 1;
    END;
    
    0 讨论(0)
提交回复
热议问题