tags:

views:

156

answers:

5

Since yesterday I started encountering errors related to date formats in SQL Server 2008.

Up until yesterday the following used to work.

EXEC MyStoredProc '2010-03-15 00:00:00.000'

Since yesterday I started getting out of range errors. After investigating I discovered the date as above is now being interpreted as "the 3rd of the 15th month" which will cause a out of range error.

I have been able to fix this using the following format with the "T".

EXEC MyStoredProc '2010-03-15T00:00:00.000'

By using this format its working fine. Basically all I'm trying to find out is if there is some Hotfix or patch that could have caused this, as all my queries using the first mentioned formats have been working for months.

Also this is not a setting that was changed by someone in the company as this is occurring on all SQL 2005/2008 servers

+1  A: 

It sounds like you've got a mixture of date styles - yyyy/MM/dd vs yyyy/dd/MM - which seem to be reverse UK and US style dates.

If all servers are showing the same behaviour it could be that just the machine executing the code has changed rather than the other machines/SQL servers in the network.

Double check that the date format or indeed culture settings are what you expect them to be on that machine. Assuming they weren't you might be able to find out what changes to the machine were made over the weekend from the event logs or Windows Update history.

ChrisF
Its not my machine as this code is being executed in SSMS so I don't see how my machine could have affected this
Eugene Niemand
@ChrisF: Neither the US or UK formats have the year first.
R. Bemrose
@Powerlord - well spotted. I'll update the post. Not sure what the format's called though.
ChrisF
@Eugene - When I said "your machine" I was assuming that it was something running locally. Replace "your machine" with the machine running the code.
ChrisF
I did check the regional settings are on UK settings as we are in the UK. Something I did however see is that the SQL server language is set to US, now from some reading I have found that when SQL Server is installed on a non US box as in our case it will default to US English and cannot be changed. You have on of two options: change the language of the user or hack the registry. Even after changing my language to British English it is still causing problems. Like I said nothing changed that we are aware of it just stopped working.
Eugene Niemand
"British" gets handled the same way as French and will cause errors for the hyphens-and-space format.
Rob Farley
+1  A: 

To avoid these kind of problems you should always specify your date format. For your purposes you should use:

SET DATEFORMAT ymd;

See this MSDN page - note the comment that

ydm is not supported for date, datetime2 and datetimeoffset data types

so you might have to use a different format if you're using one of those data types.

Mark Pim
I know that but I'm trying to figure out what happened that caused this to start failing overnight
Eugene Niemand
+2  A: 

The language setting of the session can change this.

--This works
set language english
select cast('2010-03-15 00:00:00.000' as datetime)

--This doesn't
set language french
select cast('2010-03-15 00:00:00.000' as datetime)

With the T in between, it always works. If you want a space, then leave out the hyphens.

--This works
set language english
select cast('2010-03-15T00:00:00.000' as datetime)

--This works
set language french
select cast('2010-03-15T00:00:00.000' as datetime)

--This works
set language english
select cast('20100315 00:00:00.000' as datetime)

--This works
set language french
select cast('20100315 00:00:00.000' as datetime)

So my guess is that your app has changed... or some setting on your client computer.

Rob Farley
If you read my question you will see that I have already fixed it. I wanted to know what would have caused this to change overnight on all SQL Servers without any admins being aware of any changes.
Eugene Niemand
Yes, I realise it's fixed. I was surmising about the cause. You might also want to see if data from earlier in the month was handled correctly - you may have data now from the 3rd of every month in the year.
Rob Farley
+1  A: 

Collation can effect dates, as well as the language.

To check what default language a server has installed, use the following SQL command:

   sp_configure 'default language'

If the resulting value is 0, the default language U.S. English. If the result is not 0, run the following SQL command to find the installed default language setting and date format used:

  select name ,alias, dateformat
   from syslanguages
      where langid =
      (select value from master..sysconfigures
         where comment = 'default language')

NOW as to the system updates updates, those were in 2007 to adjust to the DST changes mandated back then. Machine specific: you CAN set the clock on a specific machine to NOT adjust for DST - check that setting per machine (Windows it is on Control Panel/Date and Time under XP for instance).

IF you choose to use the dashes, you may have issues in dates. If you take out the dashes, SQL Server will never misinterpret the data:

EXEC MyStoredProc '2010-03-15 00:00:00.000'  

this is likely getting a time with a negative hour offset, which is in this case invalid.(just a guess) vs

EXEC MyStoredProc '20100315 00:00:00.000' 

Note that the T in there is the ISO8601 format, and thus the dashes are allowed.

Mark Schultheiss
Not sure why this was downvoted, all accurate information and relevent to date formats...
Mark Schultheiss
Dont know either why it was downvoted, but at last someone is giving me a bit more info about hotfixes and updates. Whould the DST update affect how the dashes are being interpreted? If this is the case I think it might be that cause I see there was a plethora of updates on 20100308 and I checked them all in MS KB and some did seem old. Do you have more info on this
Eugene Niemand
I am not sure whether they made changes specific to the dashes, however the change in 2007 was related to an US goverment change to the dates of DST - I believe it was 3 weeks longer and 3 weeks sooner on each end, which was this past weekend. Here is a link to the actual dates, some for US and some for Europe: http://support.microsoft.com/gp/cp_dst
Mark Schultheiss
Here is an extended list of products impacted by DST updates: http://support.microsoft.com/gp/dst_prodlist
Mark Schultheiss
and HERE is the February cumulative update for DST: ://support.microsoft.com/kb/979306
Mark Schultheiss
Thanx I had a look but it doesn't really answer my question. I guess this is gona be one of those unknown things that will never be resolved
Eugene Niemand
Even though this does not answer the question I'm just accepting it as your the only person that kept responding and tried to help with exactly what I asked.
Eugene Niemand
Ok I just read a post over at Simons SQL Blog (http://cli.gs/hqaR4) about this specific issue. So one solution to this problem is to Use the new DATETIME2 datatype as it understands languages that doesn't use MDY dates
Eugene Niemand
A: 

Ok I just read a post over at Simons SQL Blog (http://cli.gs/hqaR4) about this specific issue. So one solution to this problem is to Use the new DATETIME2 datatype as it understands languages that doesn't use MDY dates

Eugene Niemand