| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

12-29-08, 06:44
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 75
|
|
|
date validation function
|
|
Hi
What is the function to validate date in the select query?
Thanks,
Waseem
|
|

12-29-08, 10:57
|
|
Registered User
|
|
Join Date: May 2007
Location: somewhere in dbforums
Posts: 221
|
|
Note: I have selected yyyymmdd format.....your SQL will depend on the type of date format which you use......
-checking yyyy lies between '0001' and '9999'
Quote:
|
SUBSTR(yyyymmdd,1,4) BETWEEN '0001' AND '9999'
|
as a constraint you can check values within acceptable year range e.g between 1900 and 2100
-checking mm lies between '01' and '12'
Quote:
|
SUBSTR(yyyymmdd,5,2) BETWEEN '01' AND '12'
|
-and the trickiest part, checking the days dd which can lie between 0 - 28,29,30,31
Quote:
|
INT(SUBSTR(yyyymmdd,7,2)) BETWEEN 1 AND INT(DAY(DATE(CHAR(INSERT(SUBSTR(yyyymmdd,1,6),5,0, '-')||'-01',10))+ 1 MONTH - 1 DAY))
|
So your final code can be -
Quote:
select
case
when SUBSTR(yyyymmdd,1,4) BETWEEN '1900' AND '2100' --Making sure that year lies within acceptable range
then
case
WHEN SUBSTR(yyyymmdd,5,2) BETWEEN '01' AND '12'
THEN
CASE
WHEN (INT(SUBSTR(yyyymmdd,7,2)) BETWEEN 1 AND INT(DAY(DATE(CHAR(INSERT(SUBSTR(yyyymmdd,1,6),5,0, '-')||'-01',10))+ 1 MONTH - 1 DAY)))
then 1
else 0
end
ELSE 0
END
ELSE 0
END CASE
FROM Tablename
|
where
1 = sucess
0 = failure
and yyyymmdd denotes the column name which needs to be validated
__________________
IBM Certified Database Associate, DB2 9 for LUW
|
Last edited by nick.ncs; 12-29-08 at 11:12.
|

12-29-08, 12:20
|
|
Registered User
|
|
Join Date: Jan 2005
Posts: 75
|
|
|
|
thanks for the code. However, I was looking for some functions that could do that?
|
|

12-29-08, 12:37
|
|
Registered User
|
|
Join Date: May 2007
Location: somewhere in dbforums
Posts: 221
|
|
well don't be lazy now.... look around as how to write UDF's and apply the above logic.....
__________________
IBM Certified Database Associate, DB2 9 for LUW
|
|

12-29-08, 13:27
|
|
Registered User
|
|
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
|
|
It all depends on what you mean by verify. What Nick gave you is a perfectly good example that would work well within a UDF. Then there is a more direct route as well:
SELECT DATE(:MY-DATE) FROM SYSIBM.SYSDUMMY1;
You can do this in different date formats as well, but its a quick easy way to find out if the date is good, You can do it cheaper using a set host variable as well.
Dave
|
|

12-29-08, 16:00
|
|
Registered User
|
|
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
|
|
Adding on top of what Dave said, you can do this SELECT statement inside a procedure. Extend the procedure with an exception handler that catches invalid formats and does whatever you want to do instead. Now you write a UDF that calls the procedure. For an example, search for SOFT_INT, which is a function written by Serge Rielau and it does the same - just for integers.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
|
|

12-29-08, 16:02
|
|
Registered User
|
|
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
|
|
Quote:
|
Originally Posted by nick.ncs
Note: I have selected yyyymmdd format.....your SQL will depend on the type of date format which you use......
-checking yyyy lies between '0001' and '9999'
|
I guess you know that this will not work reliably because:
Code:
$ db2 "VALUES CASE WHEN '1ABC' BETWEEN '0001' AND '9999' THEN 'match' ELSE 'no match' END"
1
--------
match
1 record(s) selected.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|