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!

0

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.