2

I have excel file that contains some junk data in date field.

the date format is in 1/2/2015 format. I'm trying to load that file into a stage table in varchar field and apply regex replace function to clean up the bad data before loading to main table

can i somebody provide me the suitable experssion for this

create table A
(
bad_date varchar2(4000)
);

insert into A
( bad_date)
values ('1/2/2005');
insert into A
( bad_date)
values ('indep');
insert into A
( bad_date)
values ('/');

commit;


 create table B
    (
    good_date date
    );

I want to use regex function to cleanse the data which is not in the date pattern. Thanks for your help!

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
user1751356
  • 565
  • 4
  • 14
  • 33

4 Answers4

3

You can come close with something like:

select (case when regexp(bad_date, '^[0-1]?[0-9]/[0-3]?[0-9]/[0-9]{4}$')
             then to_date(bad_date, 'MM/DD/YYYY'
        end) as converted_date
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
3

Use the following:

INSERT INTO B (GOOD_DATE)
  SELECT TO_DATE(BAD_DATE, 'DD/MM/YYYY')
    FROM A
    WHERE REGEXP_LIKE(BAD_DATE, '[0-9]+/[0-9]+/[0-9]+')

SQLFiddle here

Best of luck.

3

Use ^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4} pattern for regexp_like conforming your date format.

Use the following insert statement to get clean date data :

insert into B
select * from
( select to_date(bad_date,
           case when 
             regexp_like(bad_date,'^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}') then 
             'DD/MM/YYYY'
           end) dateConverted          
    from A)
where dateConverted is not null;    

SQL Fiddle Demo

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
2

I am inclined to contribute a more mature regex to match valid dates in m/d/yyyy format:

INSERT INTO B (GOOD_DATE)
  SELECT TO_DATE(BAD_DATE, 'DD/MM/YYYY')
    FROM A
    WHERE REGEXP_LIKE(BAD_DATE,
            '^(0?[1-9]|[12][0-9]|3[01])\/(0?[1-9]|1[012])\/(19|20)[0-9][0-9]$'
    )

SQLFiddle

Inspired by

wp78de
  • 18,207
  • 7
  • 43
  • 71