generate days from date range

后端 未结 29 2416
渐次进展
渐次进展 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 06:10

    if you want the list of dates between two dates:

    create table #dates ([date] smalldatetime)
    while @since < @to
    begin
         insert into #dates(dateadd(day,1,@since))
         set @since = dateadd(day,1,@since)
    end
    select [date] from #dates
    

    *fiddle here: http://sqlfiddle.com/#!6/9eecb/3469

提交回复
热议问题