Conversion failed when converting date and/or time from character string while inserting datetime

后端 未结 15 1328
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 09:34

I was trying to create a table as follows,

create table table1(date1 datetime,date2 datetime);

First I tried inserting values as below,

相关标签:
15条回答
  • 2020-11-22 09:43

    Whenever possible one should avoid culture specific date/time literals.

    There are some secure formats to provide a date/time as literal:

    All examples for 2016-09-15 17:30:00

    ODBC (my favourite, as it is handled as the real type immediately)

    • {ts'2016-09-15 17:30:00'} --Time Stamp
    • {d'2016-09-15'} --Date only
    • {t'17:30:00'} --Time only

    ISO8601 (the best for everywhere)

    • '2016-09-15T17:30:00' --be aware of the T in the middle!

    Unseperated (tiny risk to get misinterpreted as number)

    • '20160915' --only for pure date

    Good to keep in mind: Invalid dates tend to show up with strange errors

    • There is no 31st of June or 30th of February...

    One more reason for strange conversion errors: Order of execution!

    SQL-Server is well know to do things in an order of execution one might not have expected. Your written statement looks like the conversion is done before some type related action takes place, but the engine decides - why ever - to do the conversion in a later step.

    Here is a great article explaining this with examples: Rusano.com: "t-sql-functions-do-no-imply-a-certain-order-of-execution" and here is the related question.

    0 讨论(0)
  • 2020-11-22 09:46

    The conversion in SQL server fails sometimes not because of the Date or Time formats used, It is Merely because you are trying to store wrong data that is not acceptable to the system.

    Example:

    Create Table MyTable (MyDate);

    Insert Into MyTable(MyDate) Values ('2015-02-29');

    The SQL server will throw the following error:

    Conversion failed when converting date and/or time from character string.

    The reason for this error is simply there is no such date (Feb-29) in Year (2015).

    0 讨论(0)
  • 2020-11-22 09:50

    You can try this code

    select (Convert(Date, '2018-04-01'))
    
    0 讨论(0)
  • 2020-11-22 09:53

    There are many formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT. Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not.

    The way to solve this is to use the (slightly adapted) ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and dateformat settings.

    The ISO-8601 format is supported by SQL Server comes in two flavors:

    • YYYYMMDD for just dates (no time portion); note here: no dashes!, that's very important! YYYY-MM-DD is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations!

    or:

    • YYYY-MM-DDTHH:MM:SS for dates and times - note here: this format has dashes (but they can be omitted), and a fixed T as delimiter between the date and time portion of your DATETIME.

    This is valid for SQL Server 2000 and newer.

    So in your concrete case - use these strings:

    insert into table1 values('2012-02-21T18:10:00', '2012-01-01T00:00:00');
    

    and you should be fine (note: you need to use the international 24-hour format rather than 12-hour AM/PM format for this).

    Alternatively: if you're on SQL Server 2008 or newer, you could also use the DATETIME2 datatype (instead of plain DATETIME) and your current INSERT would just work without any problems! :-) DATETIME2 is a lot better and a lot less picky on conversions - and it's the recommend date/time data types for SQL Server 2008 or newer anyway.

    SELECT
       CAST('02-21-2012 6:10:00 PM' AS DATETIME2),     -- works just fine
       CAST('01-01-2012 12:00:00 AM' AS DATETIME2)   -- works just fine  
    

    Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server.

    0 讨论(0)
  • 2020-11-22 09:53

    the best way is this code

    "select * from [table_1] where date between convert(date,'" + dateTimePicker1.Text + "',105) and convert(date,'" + dateTimePicker2.Text + "',105)"
    
    0 讨论(0)
  • 2020-11-22 09:53

    The datetime format actually that runs on sql server is

    yyyy-mm-dd hh:MM:ss
    
    0 讨论(0)
提交回复
热议问题