Check dates are valid in sql

Tearing my hair out over a query that involves manipulating and querying dates from SQL. Amongst various stuff I came across was this useful little snippet, if you have stored dates in a text field (eg nvarchar) it can tell you if the dates are valid:

set dateformat mdy

select *
from t
where isdate(startdate) = 1
    and convert(datetime, startdate) not between '19000101' and '20790606'

Keeping this one in the toolbox for another day.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s