sql >> Base de Datos >  >> RDS >> Oracle

Expresión regular en Fechas en Oracle

Prueba este:

with t(date_col) as (
select '01/01/2014' from dual
union all
select '1/2/2014' from dual
union all
select '01/3/2014' from dual
union all
select '1/04/2014' from dual
union all
select '11/1/14' from dual)
select date_col,
       case
         when regexp_instr(date_col, '^\d/\d/\d{4}$') = 1 then
          'd/m/yyyy'
         when regexp_instr(date_col, '^\d{2}/\d/\d{4}$') = 1 then
          'dd/m/yyyy'
         when regexp_instr(date_col, '^\d/\d{2}/\d{4}$') = 1 then
          'd/mm/yyyy'
         when regexp_instr(date_col, '^\d{2}/\d{2}/\d{4}$') = 1 then
          'dd/mm/yyyy'
         else
          'Unknown format'
       end date_format
  from t;

DATE_COL   DATE_FORMAT
---------- --------------
01/01/2014 dd/mm/yyyy
1/2/2014   d/m/yyyy
01/3/2014  dd/m/yyyy
1/04/2014  d/mm/yyyy
11/1/14    Unknown format