0

I can't work out what has changed to only just start getting this error

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

I have checked that the language of the SQL Server instance is "British English" I have checked that the language of the user is "British English"

What have I missed?

select * from table where updated <= '15/09/2012'

If I run set dateformat dmy, it works, but obviously only at a session level. I need to fix it for the server

7
  • See SET DATEFORMAT OR use format yyyy-mm-dd Commented Sep 22, 2014 at 12:47
  • Do you have the option to explicitly cast your date literal?, i.e. change the SQL syntax Commented Sep 22, 2014 at 12:47
  • But why use that format and (a) not the ISO Format that alawys works or (b) use a parameter Commented Sep 22, 2014 at 12:47
  • Did you try where updated <= '2012-09-15' Commented Sep 22, 2014 at 12:47
  • What if you pass the date in this format - 2012-09-15 Commented Sep 22, 2014 at 12:48

2 Answers 2

2

A better approach which eliminates all ambiguity is to use ISO 8601 formatting for all your dates - 2012-09-15. And that will work regardless of your regional settings.

Sign up to request clarification or add additional context in comments.

7 Comments

While probably right, it's not particularly helpful - this would involve rewriting a fair bit of the code, where it just needs to work for the moment...
You can do it right, or you can do it twice. Which one is more expensive?
I'm not sure why my question itself is being brought in to question...currently, doing it twice is more expensive. There isn't the budget available for someone to go through the codebase and replace all date instances. It's an inherited codebase, and it's not very well built
An even better option is to use parameterized queries instead of passing date literals. That avoids the date format ugliness, is more secure, and provides performance benefits.
Because your imagined solution is merely a band-aid, and a weak one at that. You have no assurance that your proposed solution won't cause problems with other queries. OTOH, the ISO format suggestion (which you've gotten 4 times now) will work in all cases. Fix the root cause - ambiguous date interpretation.
|
0

select * from table where convert(date,updated,105) <= '15/09/2012'

can you check above query?. I hope it will work.

 declare @updated date = GetDate()

 select 
 case when (convert(date,'15/09/2012', 105) <= @updated) then 1
 else 2 end

this query is giving 1 as output, so i guess it would work for your case also.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.