Oracle Date Comparison in Character Column

I have a query I am trying to write against a custom table within PeopleSoft.  This custom table has some character columns that are free form, but in this case storing dates, so the user can type something like "03/14/2017" for March 14, 2017.  The query needs to evaluate these entries as date criteria, determining whether the date in the column is less than the current date.

This is a little challenging because the field could be populate or it could be empty, although in PeopleSoft empty actually means a single space character.  We are also assuming that if it is populated it is always in the format MM/DD/YYYY.

A couple different approaches are below, but obviously there are huge issues with this type of implementation... what happens is someone enters something that doesn't even look like a date?  Probably an error.

At least in my case the data entry is somewhat controlled.  In both examples I do want to return rows where the date column is blank or the date meets the criteria (i.e. is less than SYSDATE).


Example 1:

SELECT *
FROM PS_B_XWLK_VAL A
WHERE 
  TO_DATE( COALESCE( A.B_XWLK_T1,'01/01/1901'),'MM/DD/YYYY')  
    <= SYSDATE
;


Example 2:

SELECT *
FROM PS_B_XWLK_VAL A
WHERE 
  (CASE WHEN A.B_XWLK_T1 = ' ' THEN
          SYSDATE 
   ELSE 
          TO_DATE( A.B_XWLK_T1, 'MM/DD/YYYY') 
   END) <= SYSDATE
;


I think the statement using COALESCE() is a little easier to follow.
COALESCE(parm1,parm2,...) will basically returns parm2 if parm1 is null or blank.  It returns the first non-null value.  So you would just make parm2 a date that will either make your expression true or false, depending what you want to get back.

The second example works similarly, but using SYSDATE as the replacement value.  You could also make it any other date value that fits your requirements, but would have to do another TO_DATE().

Maybe not ideal, but I can add either option to my PeopleSoft Query as an expressions.

Comments

Popular posts from this blog

PeopleSoft PUM Image Maintenance

Getting Client IP Address in PeopleSoft

Records in a PeopleSoft Component