0

How can we compare a date to a format in Oracle? Something like this: if MyDate is on format DD MONTH YYYY THEN /....

elsif MyDate is on format YYYY-MONTH-DD Then...

EDIT: My dates are in varchar2 and i want to keep them that way. I want just to know how to write a regex that would reprensent for example 10 October 2010.

Is it possible ? If it is a regex how would its format be please

1 Answers1

0

Echoing what was mentioned in the comments to your question, best practice would be to have an actual DATE type field instead of VARCHAR2, and if you needed specific display formats, store those in another field as a format pattern. That said, you can use REGEXP_LIKE to check the format using the patterns in the below example.

with dateinfo as (
    select 1 as id, '2018-MARCH-10' as dtString from dual
    union all
    select 2 as id, '10 MARCH 2018' as dtString from dual ) 
select id, dtString, 
    case 
        when regexp_like(dtString, '^[0-9]{4}-.[a-zA-Z]{3,}-.[0-9]{1,2}$') then 'format1'
        when regexp_like(dtString, '^[0-9]{1,2} [a-zA-Z]{3,} [0-9]{4}$') then 'format2'
        else 'no format'
    end as dtFormat 
from dateinfo;
gmiley
  • 6,531
  • 1
  • 13
  • 25