I have a query with hard coded dates, in this format
startdate >= '2012-11-03' AND enddate <= '2012-11-30 23:59'
My database date format is ‘mdy’, however I’m sure it will accept yyyy-mm-dd as its the universal date structure.
When I try run this query in SSMS on my target DB connected with a specific database user (userX) I get an error about the date formats
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
However, when I run the exact query connected as the SA user, the query executes..
Why is this? I have given userX full dbo permissions (sysadmin etc) and still get the error?
If you need to specify datetimes using strings, you should use a safe, language-independent format.
In SQL Server, that’s the ISO-8601 format (slightly adapted), and it supports basically two safe formats for
DATETIMEthat always work – regardless of your language, regional and dateformat settings:Note:
the first date-only format has no dashes or delimiters!
the second format has dashes for the date (can be omitted, too), and there’s a fixed
Tas delimiter between the date and the time portion of the stringUpdate: as per your last comment (on the different default languages for the two users) – try this:
Works just fine…
This tries to interpret the string as 11th of the 30th month of 2012 and obviously, that fails….