There are many formats supported by SQL Server - see the MSDN Internet Books on CAST and CONVERT . Most of these formats are dependent on from what settings you have, so these settings can work several times, and sometimes not.
The way to solve this problem is to use the (slightly adapted) ISO-8601 format supported by SQL Server - this format always works - regardless of your SQL Server and setting the date and time.
The ISO-8601 format is supported by SQL Server in two versions:
YYYYMMDD only for dates (without time part); Pay attention here: no dash! , it is very important! YYYY-MM-DD NOT dependent on dateformat parameters on your SQL Server and will NOT work in all situations!
or
YYYY-MM-DDTHH:MM:SS for dates and times - note: this format has a dash (but they can be omitted) and a fixed T as a separator between the date and time of your DATETIME .
This is true for SQL Server 2000 and later.
So, in your specific case - use the following lines:
insert into table1 values('2012-02-21T18:10:00', '2012-01-01T00:00:00');
and you should be fine (note: for this you need to use the international 24-hour format, not the 12-hour AM / PM format).
Alternatively : if you are on SQL Server 2008 or later, you can also use the DATETIME2 data type (instead of just DATETIME ) and your current INSERT will work without problems! :-) DATETIME2 much better and much less picky about conversions - and in any case recommend date and time data types for SQL Server 2008 or later.
SELECT CAST('02-21-2012 6:10:00 PM' AS DATETIME2),
Do not ask me why this whole topic is so complex and somewhat confusing - it is. But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat parameter on your SQL Server.
marc_s Jan 02 '13 at 8:51 2013-01-02 08:51
source share