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 : 0 (Zero)
----Valid date
SELECT ISDATE('12/12/20007)'
RETURNS : 1 (ONE)
----Invalid DataType
SELECT ISDATE('SQL')
RETURNS : 0 (Zero)
Reference : Pinal Dave (http://blog.SQLAuthority.com) , BOL




[...] Aug 11th, 2007 by pinaldave I received quite a good feedback about my post about SQL SERVER – Validate Field For DATE datatype using function ISDATE() [...]
Just be aware that the isdate() will not work with the new “date” datatype in SQL 2008
–TheDate is a legacy varchar(10) column that allowed users to enter a date, 2/29/07, 02-29-2007, Today, Daily, etc…
–Why does ISDATE(TheDate) = 1 not restrict the result set before it does the cast?
–Conversion failed when converting character string to smalldatetime data type
SELECT
ID,
UserID,
TheDate
FROM
Table1
WHERE
ISDATE(TheDate) = 1
CAST(TheDate AS DATETIME) >= @Start_Date
–Instead I have to do this…
SELECT
ID,
UserID,
TheDate
FROM
Table1
WHERE
ISDATE(TheDate) = 1
AND TheDate ‘Daily’
AND TheDate ‘Today’
AND CAST(TheDate AS DATETIME) >= @Start_Date
FYI, ISDATE() is not reliable
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/09/24/hadle-isdate-with-care.aspx
I ran into a similar problem that ran a little deeper.
I was converting some data that came from one of our Canadian clients and our office is in the U.S.
The problem I had with the IsDate function is that Canadian dates stored as VARCHAR(8) usually assume the French date style of DD/MM/YY. When trying to convert them, they would fail if the date was invalid.
Using IsDate from and American computer transposes the month and day field and also returns 0 for quite a few legit dates. As a result of this, I was unable to check for invalid dates.
I had to wrap most of my sql script inside a couple lines that temporarily change the local language to get the correct results from the IsDate function.
SET LANGUAGE French
–my code
SET LANGUAGE English
Is there any other way to do this without changing the language?