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


I have the following part of inline SQL that I have run from a C # windows service:

 < Code> Update TABLE_NAME set status_cd = '2', sdate = cast ('03 / 28/2011 18:03:40 'as DATETIME), bat_id =' 33acff9b-e2b4-410e-baaf-417656e3c255 ', cnt = 1, ATTEMPT_DATE = CAST ('03 / 28/2011 18:03:40 'AS Datetime) where id =' 1855 '  

When I have a SQL Server database from within this application , I get the following error:

System.Data.SqlClient.SqlException: A datetime data Conversion of type varchar data types occurred in an out-of-range price The statement has been terminated.

But if I take a piece of SQL and run it from SQL Management Studio, then it will run without problems.

Any idea that might be the issue?

The suspicious date format is interpreted according to the login language. This works

  ('03 / 28/2011 18:03:40 'as DATETIME) DateFormat mdy selection cast set   

It does not

  set Date Format Select DMI Cast ('03 / 28/2011 18:03:40 'AS Date Time)   

If you are right Use parameterized queries with datatype. Avoid these issues You can also use the "unlimited" format yyyyMMdd hh: mm: ss

Comments