Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date validation

Status
Not open for further replies.

extempore

Programmer
Jun 1, 2001
71
US
Is there a way to validate a date field in Sybase. I am not able to capture the @@error since the variable I am getting the date value happened to be a char field and my convert function when fails prevents the process in executing any further. Same is the case with using DateAdd, DateDiff functions. If someone could suggest a way to validate date in sybase that would be of great help.
 
Can you convert it to a datetime and compare it the valid Sybase date ranges? If it is not between your valid dates you can use RAISERROR and create your own error handling routine. Or do I totally not understand what you mean?
 
No Nicatt1_2, the problem is I cannot convert it in the first place becos the date is bad (e.g Febraury 31 2001). I cant even capture the @error as it bombs and doesnt go any further. Hope this explains.
 
I got around the same problem (bad February dates in char fields) by validating as a string.

For example, when the date is in YYYYMMDD format e.g. ("20000231"):
Code:
If substring(@date, 5, 2) = "02" and convert(int, substring(@date, 7, 2)) > 29
   print "error"

Transact-SQL isn't made for this stuff - it would be better to validate the original import file with Perl or C. But sometimes, that simply isn't available. If anyone knows of a better way in SQL, I'd like to hear it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top