Link to home
Start Free TrialLog in
Avatar of rmk
rmk

asked on

What Does DB@ Date function return for invalid date string

The column mycolumn in table mytable is varchar(10) and has entries like 01/01/2013. However some rows have invalid data in mycolumn, e.g. 'garbage'

The following sql statement shows IsDate=1 for good dates but does not show 0 for invalid dates. What am I doing wrong?

Select mycolumn,
Date(mycolumn) as mydate,
case when Date(mycolumn) is null then 0 else 1 end as IsDate
from mytable
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi rmk,

To some extent, It'll depend on which version of DB2 you're using.  

On LUW, passing an illegal value into the DATE function throws an exception.  SQL0180N -- The syntax of the string representation of a datetime value is incorrect.


Kent
Avatar of rmk
rmk

ASKER

Kent, I guess that means that I can't detect and handle the error in the SELECT statement.
Not easily.  DB2 will allow quite a few formats, YYYYDDD, MM/DD/YYYY, integer, etc. and figure out the correct date from the string, but it expects something valid.

One often used option is to write a UDF to do the validity testing.  This allows you the flexibility to validate and/or convert a lot of date formats, even custom ones.

What date format are you expecting?  I'll be glad to whip up a UDF for you.


Kent
Actually, you can also write a small stored procedure to do it automatically.  Write an exception handler that returns NULL for an invalid date.  Of course, the procedure might not be very flexible with regards to your needs.
Avatar of rmk

ASKER

The table actually a staging table for importing data, so it can have virtually anything in the columns and all the columns are defined as varchar. I'm simply trying to determine if there is a valid date string in mycolumn.
My SELECT statement is actually a pass through query in a Microsoft Access front end. I think the easiest solution for me is going to be put that query inside a native Microsoft Access query where I can then use the VBA IsDate function on mycolumn to get what I want.
Probably.  Though I'm not a big Access fan.  (Too much data in my world.)

SQL Server, MySQL, etc. all have date validation functions.  I'm thinking that it's time for DB2 to catch up and add them to the system repertoire.


Kent
Avatar of rmk

ASKER

I've requested that this question be deleted for the following reason:

No one has provided an answer and I have found a work around
The answer may not be what you wanted to hear, but it's the correct answer.
Avatar of rmk

ASKER

I respectfully disagree. No one answered the question about what does the Date function return when it causes error SQL0180N. It has to return some value because it shows some value in the result set.
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial