Create SQL Query to bring results from a field which does not follow a required format

hmmm....In a particular table, namely 'Invoice', I have a field 'Due Date', by mistake there was no condition set for entering the data in the field from the application side because of which there are now wrong faulty data in that particular field. Now I need to inform users to rectify their mistake but for that I need to select the faulty set of data.

So I need a select query which should be able to show data from the field 'Due Date' if it does not follow the format, "(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/(19|20)\d\d"

I am not a database guy, hence not sure if I am making sense, please tell me if more information is needed.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

sdstuberConnect With a Mentor Commented:
If this is oracle 10g or higher then you've pretty much already got it

select * from invoice where not regexp_like(due_date,'(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/(19|20)\d\d')

if it's 9i or lower then regular expressions aren't supported
you'll have to write your own function then

try somthing like this...

select * from invoice where check_date_str(due_date) is null
    v_temp   DATE;
    v_temp   := TO_DATE(p_str, 'mm/dd/yyyy');
    IF v_temp >= TO_DATE('1900-01-01', 'yyyy-mm-dd')
   AND v_temp <= TO_DATE('2099-12-31', 'yyyy-mm-dd')
        -- valid date string and within 20th/21st centuries
        RETURN v_temp;
        -- valid date string but not wihtin legal range
        RETURN NULL;
    END IF;
        -- not a valid date string
        RETURN NULL;

Open in new window

Raja Jegan RConnect With a Mentor SQL Server DBA & ArchitectCommented:
In 11g, you have a built in function named ISDATE to check whether a given value is a valid date or not.

In the earlier versions, we can achieve a similar thing using user defined functions.
A simple function like this will do:

Now run this query to find out which are all invalid records:

select * from urtable
where isdate(datecol) = 1
create or replace function isdate(p_inDate date, p_format varchar2) return number
 v_dummy date;
 select to_date(p_inDate,p_format) into v_dummy from dual;
 return 0;
 when others then return 1;
end isdate;

Open in new window

Raja Jegan RSQL Server DBA & ArchitectCommented:
Small Update:

Now run this query to find out which are all invalid records:

select * from urtable
where isdate(datecol, dateformat) = 1
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

note the format above also restricts the date range to be within 20th or 21st century

merely checking for valid dates alone is not enough, they have to be valid and within the proper range
I don't know about the isdate function in Oracle,  I don't see it in the 11g SQL Reference and it's not recognized in my db.

Is it documented and supported?  If so, where?  I'd like to read more about the api for it.  
Does it come with a certain patch, if so, which one?  It would be a nice addition to the sql language.
Raja Jegan RSQL Server DBA & ArchitectCommented:
   I am using Oracle 10g and it don't have it.
When I searched for this asker, I found out that this is available in 11g.

Please find the reference below for your information

Hope I clarified you out.
ok, you have to have the separately licensed OLAP stuff.  Which I do not in my db.

it doesn't appear that isdate has the flexibility the asker is looking for
that is, you can't use it to specify a particular format and determine if the string follows that format
and, if you don't have OLAP you can't use it anyway.

but, thanks for the info anyway, the OLAP stuff is interesting even if not applicable to this question specifically
one additional note

using regular expressions like above does allow you to check the format
but, even though it will enforce the centuries and look "mostly" like a date
it's still completely reliable.

'02/31/2009'  will pass the regular expression check but the string does not represent a valid date.
Raja Jegan RSQL Server DBA & ArchitectCommented:
   But the equivalent one given in my comment no 24016216 handles it out right.
yeah, same as my check_date_str except I include the century check
same idea and easy to include in yours too

That last note about regulare expressions wasn't directed to you, it was to the asker.  I wanted to make sure that even though I showed how to use the expression posted in the original question, it still wasn't a reliable way of finding good date strings
MadhusudanbanikAuthor Commented:
Though I am gonna accept both the solutions, since both of them gave the same result with the use of the additional function, but for some reason, none of them were able to show only the results which do not follow the format, it showed me all the results where duedate had any date. Luckily checking the results I found out that results not in proper format are only in tens and hence was able to rectify them manually.
" none of them were able to show only the results which do not follow the format, "

how were you using the function?

please provide an example that did not work
MadhusudanbanikAuthor Commented:
Hi sdstuber,

I am using toad, and ran the function through schema browser, then ran the query in normal editor. Even this query worked, since it was 10g but again with same result, i.e gave all the results which had any date in duedate column.

"select * from invoice where not regexp_like(due_date,'(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/(19|20)\d\d')"

My assumption is now that, the format is basically for the application side where it checks for the input from user whenever the field is a date, for some reason, for this particular field 'duedate', this format criteria was not put in on the application side and Oracle/sql cannot differentiate based on this format?

The faulty dates were like this '07/31/0207' or '11/21/0007'.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.