views:

22

answers:

2

SQL Server 2005

How to check if given date is valid smalldatetime? Before converting datetime to smalldatetime, I already know that the value is valid datetime. But while converting datetime to smalldatetime, it is raising overflow error.

So I wanted to check if the value is valid smalldatetime and if it is then convert to smalldatetime else return NULL.

Thanks in advance.

+2  A: 

There is a function here Trouble With ISDATE And Converting To SMALLDATETIME that you can use, name is fnIsSmallDateTime

There are also examples of how to use it

SQLMenace
+1 Was just typing an example, pretty much the same thing
AdaTheDev
+1  A: 
SELECT CASE WHEN @yourDateTime BETWEEN '19000101 00:00:00.000'
                                   AND '20790606 23:59:29.997'
            THEN CAST(@yourDateTime AS SMALLDATETIME)
            ELSE NULL END
LukeH