GOCHESTER.COM
Search:
 
 
Membership
 
  Member Login
Become a Member
Reset Password
 
     

 
Site Links
 
  Index
News Articles
View Articles By Category
 
     

 
SQL SERVER - Validate Field For DATE datatype using function ISDATE()
Category: Database related articles
By: Chester Zhang - July 29th, 2008
This article has been read: 97 times.


This article is based on the a question from Jr. Developer at my company. He works with the system, where we import CSV file in our database. One of the field in the database is DATETIME field. Due to architecture requirement we insert all the CSV fields in the temp table which has all the fields VARCHAR. We validate all the data first in temp table (check for inconsistency, malicious code, incorrect data type) and if passed validation we insert them in the final table in database.


We always checked DATETIME field for incorrect data type. One of the user input date as 30/2/2007. The date was sucessfully inserted in the temp table but while inserting from temp table to final table it crashed with error. We had now task to validate incorrect date value before we insert in final table. Jr. Developer asked me how can he do that? We check for incorrect data type (varchar, int, NULL) but this is incorrect date value. Regular expression works fine with them because of mm/dd/yyyy format.


After long history of the problem the solution is very simple. We now use ISDATE() function to validate if the date is valid.


Examples:


----Invalid date
SELECT ISDATE('30/2/2007')
RETURNS (Zero)
----Valid date
SELECT ISDATE('12/12/20007)'
RETURNS (ONE)
----Invalid DataType
SELECT ISDATE('SQL')
RETURNS (Zero)


Reference : Pinal Dave(http://www.SQLAuthority.com) , BOL



Back to News Articles
View Articles From Database related articles Category