generate days from date range

后端 未结 29 2490
渐次进展
渐次进展 2020-11-21 05:19

I would like to run a query like

select ... as days where `date` is between \'2010-01-20\' and \'2010-01-24\'

And return data like:

29条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-21 05:55

    MSSQL Query

    select datetable.Date 
    from (
        select DATEADD(day,-(a.a + (10 * b.a) + (100 * c.a)),getdate()) AS Date
        from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4
         union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    
        cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4
         union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    
        cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4
         union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
    ) datetable
    where datetable.Date between '2014-01-20' and '2014-01-24' 
    order by datetable.Date DESC
    

    Output

    Date
    -----
    2014-01-23 12:35:25.250
    2014-01-22 12:35:25.250
    2014-01-21 12:35:25.250
    2014-01-20 12:35:25.250
    

提交回复
热议问题