views:

30

answers:

3

I have a table where unfortunately a number of dates are stored as strings.

I have a number of reports that cast them to datetimes and filter by them. This was working fine until today when all of a sudden i'm getting this error

"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."

The dates are all stored in the format of "yyyy-mm-dd" and are all valid.

If I run the following SQL statement

SELECT CAST('2010-06-02' AS DateTime) 

I would expect to get "2010-06-02" however as of today I'm getting "2010-02-06" something has changed with the way SQL formats dates. I've had a look in regional settings on the server and it all looks to be correct.

What else could be causing this?

+2  A: 

Try setting the format explicitly

select convert(datetime, '2010-06-02',101)
Barry
@Gavin - it could be that the Language has been changed on the Database. http://msdn.microsoft.com/en-us/library/ms191448.aspx
Barry
@Gavin - some further info on Date and Time in SQL Server http://msdn.microsoft.com/en-us/library/ms180878.aspx
Barry
@Gavin Draper That impression was close to the real thing. It's the date string without hyphens that gets interpreted ignoring any culture settings that you might have.
Denis Valeev
A: 

Q1: What else could be causing this?

The local, You probably are under the local101(US) and put data from 103 (British/French)

Like barry sad use convert

Vash
A: 

An unambiguous way of getting this conversion is to do the following:

SELECT CAST(replace('2010-06-02', '-', '') AS DateTime) 

And that will always be interpreted as YYYYMMDD, ignoring the set dateformat ydm declaration or any cultural settings that the database has.

Denis Valeev