Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

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.
0
Madhusudanbanik
Asked:
Madhusudanbanik
  • 8
  • 4
  • 2
2 Solutions
 
sdstuberCommented:
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
CREATE OR REPLACE FUNCTION check_date_str(p_str IN VARCHAR2)
    RETURN DATE
IS
    v_temp   DATE;
BEGIN
    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')
    THEN
        -- valid date string and within 20th/21st centuries
        RETURN v_temp;
    ELSE
        -- valid date string but not wihtin legal range
        RETURN NULL;
    END IF;
EXCEPTION
    WHEN OTHERS
    THEN
        -- not a valid date string
        RETURN NULL;
END;

Open in new window

0
 
Raja Jegan RSQL 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
as
 v_dummy date;
begin
 select to_date(p_inDate,p_format) into v_dummy from dual;
 return 0;
exception
 when others then return 1;
end isdate;

Open in new window

0
 
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sdstuberCommented:
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
0
 
sdstuberCommented:
rrjegan17,
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 11.1.0.7 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.
0
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
sdstuber,
   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

http://download.oracle.com/docs/cd/B28359_01/olap.111/b28126/dml_functions_1106.htm

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

Thanks!
0
 
sdstuberCommented:
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
0
 
sdstuberCommented:
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.
0
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
sdstuber,
   But the equivalent one given in my comment no 24016216 handles it out right.
0
 
sdstuberCommented:
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
0
 
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.
0
 
sdstuberCommented:
" 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
0
 
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'.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 8
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now