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

0 votes

I tried creating a table as follows:

create table table1(date1 datetime,date2 datetime);

I tried inserting values as below:

insert into table1 values('21-02-2012 6:10:00 PM','01-01-2001 12:00:00 AM');

I encountered this error:

Cannot convert varchar to datetime.

I tried this as a solution :

insert into table1 values(convert(datetime,'21-02-2012 6:10:00 PM',5)

                          ,convert(datetime,'01-01-2001 12:00:00 AM',5));

But still getting the error saying:

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

Can someone help me with this?

Apr 29, 2022 in Other DevOps Questions by Kichu
• 19,040 points
14,873 views

1 answer to this question.

0 votes

The SQL server supports many formats. Most of the formats are dependent on what settings you have. To solve your issue you can use the ISO-8601 date format that is supported by SQL Server. 

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

  1. YYYYMMDD for just dates.
  2. YYYY-MM-DDTHH:MM:SS for dates and times.

In your specific case - use these strings:

To know more about SQL, It's recommended to join Microsoft SQL Training today.

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

I hope this helps you.

answered Apr 30, 2022 by narikkadan
• 63,600 points

Related Questions In Other DevOps Questions

+1 vote
2 answers

When do we use Chef or Azure SDK to create VM and deploy in automation

The solution to the automated deployment in ...READ MORE

answered Aug 21, 2018 in Other DevOps Questions by Priyaj
• 58,020 points
1,142 views
0 votes
1 answer

salt error : “TypeError: coercing to Unicode: need string or buffer, bool found”

You can check the discussion regarding same ...READ MORE

answered Jun 17, 2018 in Other DevOps Questions by shubham
• 7,340 points
2,416 views
0 votes
1 answer

Start vNext build from Powershell and get artefacts

TFS 2015 comes with the new REST API, ...READ MORE

answered Jul 5, 2018 in Other DevOps Questions by Kalgi
• 52,350 points
818 views
0 votes
0 answers
0 votes
1 answer

Calculate Time Intersection to Correlate Sequences of Independent Events

I think this solution requires a CROSS JOIN implementation. ...READ MORE

answered Oct 26, 2018 in Power BI by Upasana
• 8,620 points
787 views
0 votes
1 answer

How do I UPDATE from a SELECT in SQL Server?

MERGE INTO YourTable T USING ...READ MORE

answered Feb 3, 2022 in Database by Vaani
• 7,070 points
819 views
0 votes
0 answers

How do I UPDATE from a SELECT in SQL Server?

INSERT INTO Table (col1, col2, col3) SELECT col1, ...READ MORE

Feb 4, 2022 in Database by Vaani
• 7,070 points
545 views
0 votes
1 answer

What is a stored procedure?

A stored procedure is a set of ...READ MORE

answered Feb 4, 2022 in Database by Neha
• 9,020 points
1,054 views
0 votes
1 answer

Terraform and Azure DevOps pipelines

This error message is normal. Usually, the ...READ MORE

answered Apr 23, 2022 in Other DevOps Questions by narikkadan
• 63,600 points
1,001 views
0 votes
1 answer

Manifest Merger failed with multiple errors in Android Studio

Open application manifest (AndroidManifest.xml)  > Merged Manifest. ...READ MORE

answered Apr 29, 2022 in Other DevOps Questions by narikkadan
• 63,600 points
1,703 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP