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

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

    For me this worked:

    INSERT INTO [MyTable]
               ([ValidFrom]
               ,[ValidTo])
           VALUES
               ('2020-01-27 14:54:11.000'
               ,'2023-01-27 14:52:50.000')
    
    0 讨论(0)
  • 2020-11-22 09:57

    Please Try this.

    SQL Server expects dates in MM/DD/YYYY format,If English is set as your default language.Here am saving datepicker value to sql2008 database.My field type is datetime in database.dpdob is my datepicker name.

               Dim test = dpdob.Text.Replace("-", "/")
               Dim parts As String() = test.Split(New Char() {"/"c})
               Dim firstPart As String = parts(0)
               Dim thirdPart As String = parts(2)
               Dim secondPart As String = parts(1)
               Dim test1 = secondPart + "/" + firstPart + "/" + thirdPart
               Dim dob = test1
    

    Now use dob in your insert query.

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

    Just update the date format as like bellow

    yyyy-MM-dd hh:MM:ss
    

    It solves the problem for me and it works fine

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

    set Culture to english from web.config file

      <globalization uiCulture="en-US" culture="en-US" />
    

    for example if you set the culture to arabic the thime will be

    ‏22‏/09‏/2017‏ 02:16:57 ص

    and you get the error:Conversion failed when converting date and/or time from character string while inserting datetime

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

    This is how to easily convert from an ISO string to a SQL-Server datetime:

    INSERT INTO time_data (ImportateDateTime) VALUES (CAST(CONVERT(datetimeoffset,'2019-09-13 22:06:26.527000') AS datetime))
    

    Source https://www.sqlservercurry.com/2010/04/convert-character-string-iso-date-to.html

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

    I'm Tried this and it's working with me :

    SELECT CONVERT(date, yourDate ,104)
    
    0 讨论(0)
提交回复
热议问题