I was trying to create a table as follows,
create table table1(date1 datetime,date2 datetime);
First I tried inserting values as below,
For me this worked:
INSERT INTO [MyTable]
([ValidFrom]
,[ValidTo])
VALUES
('2020-01-27 14:54:11.000'
,'2023-01-27 14:52:50.000')
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.
Just update the date format as like bellow
yyyy-MM-dd hh:MM:ss
It solves the problem for me and it works fine
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
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
I'm Tried this and it's working with me :
SELECT CONVERT(date, yourDate ,104)