Don’t use ISDATE() function to check valid Date

I got the below email from one of my teammate on ISDATE() function.

date-email

Thanks to him for asking me the question. I do not have any idea on this behaviour of  sql server. I gone through the MSDN documentation and  got to  know, why SQL Server behaves like this.

select ISDATE(’08/16/013′) returns true because here it treats ’08/16/013′ as datetime type (where time part is ’00:00:00.000’ which is optional in datetime data type) But when you use select Convert(date, ’08/16/013′), ’08/16/013′ is not a valid date format, it is a valid datetime format.

Date data type supports only either 2 digits or 4 digits’ year. So instead you use

select Convert(date, ’08/16/13′) or select Convert(date, ’08/16/2013′).

Date data type supports only either 2 digit or 4 digit year string literals whereas for datetime data type there is no restriction like date type. The string literal will be number separator number separator number separator [time][time] and it validates based on what dateformat set for the instance.

Below is the excerpt from MSDN:

Supported String Literal Formats for date

date data type

 

Data Platform Virtual Summit 2020

Subscribe to SQLServerGeeks YouTube channel. If you want more learning content in your inbox, subscribe to SQLServerGeeks Bulletin.

SQLServerGeeks YouTube | SQLServerGeeks Bulletin | SQLServerGeeks Twitter

Avatar

About Sandip Pani

Sandip Pani is a Database/BI developer and speaker and is located in Bangalore, India. He has been working with SQL Server over 11 years. He is MCITP for SQL Server 2008 and specializes in SQL Server for application developers and performance and query tuning. His specialization is into Query Tuning and performance troubleshooting. He is working as Senior Technical Specilist for NextGen Healthcare. He is active in SQL community world. He share and enhance his knowledge of SQL Server by spending time at newsgroups and forums, reading and writing blogs, and attending and speaking at conferences.

View all posts by Sandip Pani →

Leave a Reply

Your email address will not be published. Required fields are marked *