SHOW ALL Dates data between two dates; if no row exists for particular date then show zero in all columns

后端 未结 6 1496
孤街浪徒
孤街浪徒 2020-12-03 06:12

I want to show all dates between two dates when there is any date data missing then its should show zero in val column .

declare @temp table (
id int ident         


        
6条回答
  •  抹茶落季
    2020-12-03 06:49

    This will work as long as there are less than 2047 days between from and to dates

    declare @from smalldatetime = '10/01/2012'
    declare @to smalldatetime = '10/15/2012'
    
    select t.id, dateadd(day, number,@from), isnull(val, 0) val from @temp t
    right join master..spt_values s
    on dateadd(d, s.number, @from) = t.CDate
    where
    datediff(day, @from, @to ) > s.number
    and s.type = 'P'
    

提交回复
热议问题