You might have used this function earlier for data validation, while working with conversions and interfaces. I came across this function while the KT section was going on. So effective to validate the incoming data from the inbound files. Further searching for the same, I landed in my colleagues blog: peoplesoft learnings blog. Below is what Ganesh has posted in his blog.
In Peoplesoft where we read data from the files, especially for inbound interface , conversion and one time data load. Date is usually read as character field in different format and converted to date. We need to validate the date for further processing. It will be good if we can validate date field in SQL (set processing) than the validating using people code (row by row). Oracle doesn't have very useful function IsDate() not sure why.
Lets see how we can do set processing for the date validation.
1) Regular expression came to my mind to check the pattern and validate the date easily.
Select 'X' from dual
Where REGEXP_LIKE ('1999-10-10','^(1920)\d\d[- /.](0[1-9]1[012])[- /.](0[1-9][12][0-9]3[01])$')
This is simple regular expression which matches only the date format. It doesn't check for leap years and 30,31 days for all the months.
2) Select 'X' from dual
Where NOT REGEXP_LIKE ('2009-2-29','^((((1920)(([02468][048])([13579][26]))-02-29))((20[0-9][0-9])(19[0-9][0-9]))-((((0[1-9])(1[0-2]))-((0[1-9])(1\d)(2[0-8])))((((0[13578])(1[02]))-31)(((0[1,3-9])(1[0-2]))-(2930)))))$')
This regular expression is quite big difficult right every time correctly. Need to copy paste where ever it is requried.
3) if the regular expression is getting difficult, then can create a PL/SQL function and use that function in the sql to validate.
we can use the above sql itself in the function. or use the following.
function IsDate (str long) return boolean is the_Date date;
begin
the_date := to_date(str,'dd-mon-yyyy'); -- note the correction to date mask from what you had below
RETURN true; -- conversion success
exception
when others then RETURN false; -- conversion failed
end;
In the Peoplesoft Implementation project we can create such function once and can be used in many SQL/Peoplecode/AE's. It is difficult to maintain this procedure in different instances like Dev,FIT,SIT,UAT,PPROD,PROD, but is it worth the effort.
Oracle SQL has Regular Expression support from 2003. Many people doesn't know about it and has not been used by many of the Peoplesoft developers and legacy SQR and Peoplecode.
These functions can simplify a lot of pattern matching work in the SQL and AE / SQR program.
REGEXP_LIKE Determine whether pattern matches
REGEXP_SUBSTR Determine what string matches the pattern
REGEXP_INSTR Determine where the match occurred in the string
REGEXP_REPLACE Search and replace a pattern
contains the details of using the regular expression iPublish Postn Oracle SQL.
http://www.oracle.com/technetwork/database/features/application-development/twp-regular-expressions-133133.pdf
Eg:-SELECT * FROM TABLENAME WHERE regexp_like (FIELDTOBECHECKED,'^[A-Za-z0-9]+$')
Can be used to select fields containing only alpha numeric character.
Pattern matching for Phone number , Postal code , Email id , National Identifier , Employee ID , Name , Addresss , Bank account number , credit card number , different identifier.
In Peoplesoft where we read data from the files, especially for inbound interface , conversion and one time data load. Date is usually read as character field in different format and converted to date. We need to validate the date for further processing. It will be good if we can validate date field in SQL (set processing) than the validating using people code (row by row). Oracle doesn't have very useful function IsDate() not sure why.
Lets see how we can do set processing for the date validation.
1) Regular expression came to my mind to check the pattern and validate the date easily.
Select 'X' from dual
Where REGEXP_LIKE ('1999-10-10','^(1920)\d\d[- /.](0[1-9]1[012])[- /.](0[1-9][12][0-9]3[01])$')
This is simple regular expression which matches only the date format. It doesn't check for leap years and 30,31 days for all the months.
2) Select 'X' from dual
Where NOT REGEXP_LIKE ('2009-2-29','^((((1920)(([02468][048])([13579][26]))-02-29))((20[0-9][0-9])(19[0-9][0-9]))-((((0[1-9])(1[0-2]))-((0[1-9])(1\d)(2[0-8])))((((0[13578])(1[02]))-31)(((0[1,3-9])(1[0-2]))-(2930)))))$')
This regular expression is quite big difficult right every time correctly. Need to copy paste where ever it is requried.
3) if the regular expression is getting difficult, then can create a PL/SQL function and use that function in the sql to validate.
we can use the above sql itself in the function. or use the following.
function IsDate (str long) return boolean is the_Date date;
begin
the_date := to_date(str,'dd-mon-yyyy'); -- note the correction to date mask from what you had below
RETURN true; -- conversion success
exception
when others then RETURN false; -- conversion failed
end;
In the Peoplesoft Implementation project we can create such function once and can be used in many SQL/Peoplecode/AE's. It is difficult to maintain this procedure in different instances like Dev,FIT,SIT,UAT,PPROD,PROD, but is it worth the effort.
Oracle SQL has Regular Expression support from 2003. Many people doesn't know about it and has not been used by many of the Peoplesoft developers and legacy SQR and Peoplecode.
These functions can simplify a lot of pattern matching work in the SQL and AE / SQR program.
REGEXP_LIKE Determine whether pattern matches
REGEXP_SUBSTR Determine what string matches the pattern
REGEXP_INSTR Determine where the match occurred in the string
REGEXP_REPLACE Search and replace a pattern
contains the details of using the regular expression iPublish Postn Oracle SQL.
http://www.oracle.com/technetwork/database/features/application-development/twp-regular-expressions-133133.pdf
Eg:-SELECT * FROM TABLENAME WHERE regexp_like (FIELDTOBECHECKED,'^[A-Za-z0-9]+$')
Can be used to select fields containing only alpha numeric character.
Pattern matching for Phone number , Postal code , Email id , National Identifier , Employee ID , Name , Addresss , Bank account number , credit card number , different identifier.
after browsing a lot of crappy blogs today, thisone really sticks out from the crowd!
ReplyDelete