Skip to content
Advertisement

SQL Finding Missing Dates Between Ranges

I have a table of jobs that have ran for different source systems. These have a “RunDate” and then the FromDate and ToDate.

I want to find the gaps where we are missing any dates in the FromDate and ToDate fields to make sure that we have covered the data in those periods.

Many examples I’ve looked at work where a single date misses in a single column of ranges, however I have a From and To range that I need to test and ultimately work out where a date may be missed.

So obviously using the example above I should be able to ascertain that the period between 2021-06-02 and 2021-06-04 for SourceSystem ILG and period 2021-06-03 to 2021-06-03 is missing for SourceSystem AAP.

Struggling to make it work for ranges, I can work with single dates but the system doesn’t log them in this fasion.

UPDATE

I took the accepted answer and then tagged some code to it to be able to explode all the individual dates between the ranges specified.

Included the code in case anyone needs in the future.

Advertisement

Answer

Try this:

Result:

db-fiddle

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement