I have a table which contains date in character format . I want to check the format of date . Please let me know how i can do this in DB2. I know there is a function ISDATE but its not working in DB2. I am on AS400 using db2 as date base .. Please help me out
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- php PDO::FETCH_ASSOC doesnt detect select after ba
- Bulk update SQL Server C#
Actually, it looks like DB2 for the AS/400 may not have the
ISDATE()
function (I can't find anything in the V6R1 reference - or, interestingly, the LUW reference either). So your problem appears to be that the function does not exist.The root of the problem, of course, is that attempting to translate an invalid date causes the statement to halt. In light of that, this statement should give you a date if the formatting was possible, and null if it was not. Please note that if you've mixed USA and EUR formats/ordering, you might not be able to correctly recover the data (if your separators are different, which I think they are by default, you'll probably be okay).
This turns a dataTable looking like this:
Into this:
This example is of course using the default formats which auto-translate. If you have something else, you'll have to manually translate it (instead of returning the format, you can substring it into ISO then cast it).
I am not clear on what "check the format of date" intends, nor do I know what any existing ISDATE() effects, but what is implied by the name seems clear enough. Consider:
Extremely lightly tested on v5r3 [i.e. only to ensure both that a few bad date character string example values and some garbage text input actually returned NULL and that each of the various standard date formats with valid values returned a date value], that the following should effect the evaluation of the input varying character string up to 10 characters as a value that can be cast to DATE; that when the input value can not be cast to date, then the result is NULL. Of course that means an initially NULL value is not directly distinguishable from the result produced for an invalid date string.
If some desired indicator, such as
TRUE
orFALSE
is desired as the result instead, then the use of the User Defined Function (UDF) could be coded in a CASE expression; e.g.:CASE WHEN ISDATE(myVCcol) IS NULL THEN 'FALSE' ELSE 'TRUE' END