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,050 points
6,537 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:

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
• 53,520 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,100 points
547 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
1,793 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
366 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
375 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,020 points
232 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,020 points
121 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
• 8,940 points
393 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
• 53,520 points
511 views
0 votes
1 answer

'git' is not recognized as an internal or external command

Add the following paths to PATH: C:\Program Files\Git\bin\ C:\Program ...READ MORE

answered Apr 29, 2022 in Other DevOps Questions by narikkadan
• 53,520 points
751 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