2

I am not able to zero in on the record which is causing the invalid month issue, would like to know is there a easier way to find the data causing the issue rather than manually checking everything one by one?

Edit:

Sample Data (Comes in as text):

1/17/2019 12:00:00 AM

Converting to timestamp:

to_timestamp(tested_date, 'MM/DD/YYYY HH:MI:SS AM')
6
  • Please provide sample data and expected results. It really depends on what your data looks like.
    – GMB
    Commented Mar 9, 2020 at 17:59
  • @GMB hey thanks, i have added sample data. Commented Mar 9, 2020 at 18:02
  • ..........Huge? Commented Mar 9, 2020 at 18:34
  • Extract the date part, aggregate and sort. Do the same for the time part. You will find the issue in seconds. Commented Mar 9, 2020 at 18:40
  • @DavidדודוMarkovitz i had tried that initially, For some reason i was not able to find out the issue.I did think its simple but was struggling before i decided to post a question here. Commented Mar 9, 2020 at 18:43

5 Answers 5

6

Starting Oracle 12.2, you can use validate_conversion() to locate invalid date strings:

select tested_date
from mytable
where validate_conversion(tested_date as timestamp, 'MM/DD/YYYY HH:MI:SS AM') = 0

From the documentation:

VALIDATE_CONVERSION determines whether expr can be converted to the specified data type. If expr can be successfully converted, then this function returns 1; otherwise, this function returns 0.

1
  • ah! this looks like a pretty simple solution but unfortunately, am on Oracle 11G now. Is there any other way to get this done ? Commented Mar 9, 2020 at 18:11
2

You can create a custom function to check each date string:

CREATE FUNCTION is_valid_date(
  date_string IN VARCHAR2,
  format_model IN VARCHAR2 DEFAULT 'MM/DD/YYYY HH12:MI:SS AM'
) RETURN NUMBER
IS
  v_date DATE;
BEGIN
  v_date := TO_DATE( date_string, format_model );
  RETURN 1;
EXCEPTION
  WHEN OTHERS THEN
    RETURN 0;
END;
/

Then use:

SELECT tested_date
FROM   your_table
WHERE  is_valid_date( tested_date ) = 0;

To find the rows that have errors on conversion.

(Note: In Oracle both DATE and TIMESTAMP data types have time components so you don't need to convert your string to a TIMESTAMP and can just convert it to a DATE. TIMESTAMP data types can have optional fractional seconds or a time zone, while DATE data types just have year through to integer second components.)

4
  • @MT0 thanks a lot. This work perfectly. Appreciate it. Commented Mar 9, 2020 at 18:38
  • @DavidדודוMarkovitz Please feel free to write your own answer or to give suggestions for improvements if you feel their are issues with answers. If you can think of another way to check for valid dates that is more performant then I'm sure the OP would be interested to know it.
    – MT0
    Commented Mar 9, 2020 at 18:45
  • I felt free to comment. Commented Mar 9, 2020 at 18:50
  • P.S. considering the time it took the OP to get back to you, a "huge" data volume is probably 1 million records Commented Mar 9, 2020 at 18:52
1

I found this solution which works.

1) Create a Function

create function test_date(d varchar2) return varchar2
is
  v_date date;
begin
  select to_timestamp(d,'MM/DD/YYYY HH:MI:SS AM') into v_date from dual;
  return 'Valid';
  exception when others then return 'Invalid';
end;

2)

select your_date_col, test_date(your_date_col)
from your_table;
0
0

Perhaps you could use regexp_like. I tried it for the sample date and it seems to work:

select '1/17/2019 12:00:00 AM' as date_str
from dual
where REGEXP_LIKE('1/17/2019 12:00:00 AM', 
                  '^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} [A-Z]{2}')

If you want to check for all the records which don't fit the format, you can use a NOT in front of the regexp_like:

select '1/17/2019 12:00:00 AM' as date_str
from dual
where not REGEXP_LIKE('1/17/2019 12:00:00 AM', 
                  '^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} [A-Z]{2}')

Basically it's checking for the following format in the string, "01/01/0000 00:00:00 AA". 0 stands for a number, A stands for an alphabet, 1 stands for an optional number (Because date and month can be 1 or 2 digits). In all other cases, the condition will fail. You can use this to get the records which are not in the required format.

Hope this helps.

2
  • This won't solve the OP's issue as it recognises 99/99/9999 99:99:99 ZZ as a valid date [db<>fiddle].
    – MT0
    Commented Mar 9, 2020 at 20:55
  • You could do a quick check for "not a valid month" without parsing the whole string. Commented Mar 9, 2020 at 23:43
0

If the strings are supposed to be in format 'MM/DD/ .....' then you could do something like this - to find strings that have anything other than 1 through 12 before the first slash (or, perhaps, 01 etc.):

select *
from   your_table
where  substr(tested_date, 1, instr(tested_date, '/') - 1) not in 
       ('1', '2', '3', ... , '9', '10', '11', '12', '01', '02', .... , '09')

(of course, spell out all the valid cases, don't use ellipses as I did)

You will still be left with the harder question: what to do with the wrong data.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.