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

后端 未结 15 1329
隐瞒了意图╮
隐瞒了意图╮ 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 10:07

    I had this issue when trying to concatenate getdate() into a string that I was inserting into an nvarchar field.

    I did some casting to get around it:

     INSERT INTO [SYSTEM_TABLE] ([SYSTEM_PROP_TAG],[SYSTEM_PROP_VAL]) VALUES 
       (
        'EMAIL_HEADER',
        '<h2>111 Any St.<br />Anywhere, ST 11111</h2><br />' + 
            CAST(CAST(getdate() AS datetime2) AS nvarchar) + 
        '<br /><br /><br />'
       )
    

    That's a sanitized example. The key portion of that is:

    ...' + CAST(CAST(getdate() AS datetime2) AS nvarchar) + '...

    Casted the date as datetime2, then as nvarchar to concatenate it.

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

    Simple answer - 5 is Italian "yy" and 105 is Italian "yyyy". Therefore:

    SELECT convert(datetime,'21-02-12 6:10:00 PM',5)
    

    will work correctly, but

    SELECT convert(datetime,'21-02-12 6:10:00 PM',105)
    

    will give error.

    Likewise,

    SELECT convert(datetime,'21-02-2012 6:10:00 PM',5)
    

    will give error, where as

    SELECT convert(datetime,'21-02-2012 6:10:00 PM',105)
    

    will work.

    0 讨论(0)
  • 2020-11-22 10:09
    convert(datetime2,((SUBSTRING( ISNULL(S2.FechaReal,e.ETA),7,4)+'-'+ SUBSTRING( ISNULL(S2.FechaReal,e.ETA),4,2)+'-'+ SUBSTRING( ISNULL(S2.FechaReal,e.ETA),1,2) + ' 12:00:00.127')))  as fecha,
    
    0 讨论(0)
提交回复
热议问题