Solved

What Does DB@ Date function return for invalid date string

Posted on 2013-12-04
10
405 Views
Last Modified: 2013-12-09
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
0
Comment
Question by:rmk
  • 6
  • 4
10 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 39696186
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
0
 

Author Comment

by:rmk
ID: 39696385
Kent, I guess that means that I can't detect and handle the error in the SELECT statement.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 39696423
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
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 45

Expert Comment

by:Kent Olsen
ID: 39696438
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.
0
 

Author Comment

by:rmk
ID: 39696615
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.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 39696626
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
0
 

Author Comment

by:rmk
ID: 39706147
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
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 39705931
The answer may not be what you wanted to hear, but it's the correct answer.
0
 

Author Comment

by:rmk
ID: 39706075
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.
0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 500 total points
ID: 39706096
Hi rmk,

It doesn't return anything, it throws an exception and sets the error code to SQL0180N.  

It would be up to the application's error handler to trap the error and substitute NULL (or any other desired value) and continue processing.


Kent
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Special characters in DB2 won't import to SQL via CSV file 9 162
db2 timestamp 5 65
How to conver to Hijri date in SSRS 6 210
AS400 BRMS - none enrolled tape 6 68
November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question