Get a list of dates between two dates using a function

前端 未结 21 1430
天涯浪人
天涯浪人 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:59

    This does exactly what you want, modified from Will's earlier post. No need for helper tables or loops.

    WITH date_range (calc_date) AS (
        SELECT DATEADD(DAY, DATEDIFF(DAY, 0, '2010-01-13') - DATEDIFF(DAY, '2010-01-01', '2010-01-13'), 0)
            UNION ALL SELECT DATEADD(DAY, 1, calc_date)
                FROM date_range
                WHERE DATEADD(DAY, 1, calc_date) <= '2010-01-13')
    SELECT calc_date
    FROM date_range;
    

提交回复
热议问题