Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there an internal table to SQL that will allow you to check dates?

The reason I ask is we have some automated uploads that are suppose to happen everyday. I would like to create a list of missing dates by comparing the upload data to a date table if there is an internal one. If not I don't want to create one so will come up with another way of doing it.

This just seemed the simplest way of doing it if there was such a table.

Thanks,

Carolyn
Posted
Comments
ZurdoDev 24-Feb-12 16:56pm    
Meaning a table that stores every single date? I don't understand the question. But no, there is no table, that I am aware of, simply holding dates.
Orcun Iyigun 24-Feb-12 17:27pm    
From OP: Yes, a table that holds every single date. I don't have to go all the way back. I just needed something verifying every date for a particular year so I can do a comparison with the uploaded dates.

I didn't think there was because I couldn't find anything online but thought I would ask in case I was missing something.

Thanks,
Carolyn
Ganesan Senthilvel 24-Feb-12 20:19pm    
You need to create the user defined table; no internal one.
Varun Sareen 25-Feb-12 0:36am    
correct

1 solution

One way to solve the problem is to create the table to check against.
SQL
With CTE(dates) as(
    SELECT  Min(dates) as dates
    FROM    Dates
    UNION ALL
    SELECT  dates + 1 as dates
    FROM    CTE
    WHERE   dates <= (SELECT Max(dates) FROM dates)
    )
SELECT  c.dates
FROM    CTE c
left outer join dates d
    ON  c.dates = d.dates
WHERE   d.dates IS NULL
Here I'm assuming a table "dates" with one column "dates" where you want to find the missing values between the first date and the last date.
It's easy enough to change the query to check between the first date and the current date by changing the condition in the second part of the CTE to sysdate or getdate instead of max(dates).
It's admittedly crude, but should work.
Don't forget to remove the time portion from the date if you want the query to work properly.
Trunc(dates) in Oracle, or CONVERT(VARCHAR(10),dates,112) in SQLServer.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900