3

I have some dates in table over of a two years like this as a example date

01-jan-2012
02-jan-2012
04-jan-2012
05-jan-2012
06-jan-2012
07-jan-2012
09-jan-2012
11-jan-2012
.
.
.
01-DEC-2012

I think you have noticed that there a missing date of 03-jan-2012 and 08-jan-2012 and the same criteria with all dates.My Question is that is there any way in oracle to find the missing dates.Plz Help !

2
  • I don't think so, Oracle will help you to find the missing dates. Since, these are data, so all you can do, write some query, which will help you to find those missing date Commented Dec 21, 2012 at 11:48
  • Can U Show Me Some Example Commented Dec 21, 2012 at 11:53

3 Answers 3

8

This will get you all missing days for one year (SQL Fiddle).

all_dates generates a list of all dates of 2012 (adjust as required), and the LEFT JOIN checking for IS NULL eliminates those dates that exist in your source-table.

WITH all_dates AS (
  SELECT TO_DATE('01-jan-2012') + ROWNUM - 1 AS d
  FROM dual
  CONNECT BY ROWNUM <= ADD_MONTHS(TO_DATE('01-jan-2012'), 12 ) - TO_DATE('01-jan-2012')
)
SELECT all_dates.d
FROM all_dates
LEFT JOIN t ON ( t.d = all_dates.d )
WHERE t.d IS NULL
ORDER BY all_dates.d
;

Make sure to use a bind variable instead of hard-coding the date three times.

Sign up to request clarification or add additional context in comments.

2 Comments

is there a possibilty to set an end date instead of the number of days? e.g. when selecting dates from 01.01.2012 to 31.12.2014 2012 will have 366 days but 2013 and 2014 only have 265 days as 2012 was a leap year.
@LPrc: I edited my answer to work for each year, no matter if leap year or not. Actually makes more sense like that, hope it helps.
1

You can generate sequence of date range you needed, then use LEFT JOIN to find missed dates.

Comments

0

Almost the same answer as @Peter, but a very slightly different version.

select all_dates.date_ missing_dates from 

(select to_date('01-Jan-14') + level - 1 date_
from dual connect by level <= 365) all_dates 

left join

((select to_date('01-Jan-14') + level - 1 date_
from dual connect by level <= 365)               

minus

(select to_date(round (dbms_random.value (1, 28))
        || '-'
        || round (dbms_random.value (01, 12))
        || '-'
        || round (dbms_random.value (2014, 2014)),
        'DD-MM-YYYY') + level - 1 random_date_1 
from dual
connect by level <= 52)) transaction_data

on all_dates.date_ = transaction_data.date_
where transaction_data.date_ is null;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.