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

后端 未结 15 1591
隐瞒了意图╮
隐瞒了意图╮ 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',
        '

    111 Any St.
    Anywhere, ST 11111


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


    ' )

    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.

提交回复
热议问题