SQL Find missing date ranges

前端 未结 2 1439
情深已故
情深已故 2021-01-03 11:23

I have a table that holds all the days/months of the year

E.G.

Day      Month
1         9
2         9
3         9
4         9
5         9
6         9         


        
2条回答
  •  迷失自我
    2021-01-03 11:51

    If you main table is

    #Temp(Title varchar(10),DateRange date)
    

    You can do like something like

    CREATE TABLE #ALLDATE(Date1 date)
    DECLARE @startDate DATE='9/1/2013'
    DECLARE @endDate DATE='9/30/2013'
    
    insert into #ALLDATE
    SELECT [Date] = DATEADD(Day,Number,@startDate) 
    FROM  master..spt_values 
    WHERE Type='P'
    AND DATEADD(day,Number,@startDate) <= @endDate
    
    
    select 'webshop',Date1 
    from #ALLDATE
    where Date1 not in 
            (select DateRange from #Temp where Title='webshop' and MONTH(GETDATE())=9)
    

提交回复
热议问题