Coding⏱️ 3 min read📅 2026-06-03

How to Fix: Conversion failed when converting date and/or time from character string while inserting datetime

Error in converting date and/or time from character string while inserting datetime

Quick Answer: Use the CONVERT function to convert varchar to datetime, for example: CONVERT(datetime, '21-02-2012 6:10:00 PM', 120)

The error 'Cannot convert varchar to datetime' occurs when attempting to insert date and/or time values from character strings into a column defined as datetime in SQL Server. This issue affects users who are trying to create tables with datetime columns and insert data using character strings.

This error can be frustrating because it prevents the successful creation of the table and insertion of data. However, by following these steps, you should be able to resolve the issue and successfully create your table.

🛑 Root Causes of the Error

  • The primary reason for this error is that SQL Server's datetime data type requires exact date and time values, not character strings. When a character string is inserted into a datetime column, SQL Server cannot accurately convert it to a datetime value.
  • An alternative reason for this error could be due to the format of the character string not matching the expected format for the datetime data type.

✅ Best Solutions to Fix It

Using the Convert Function

  1. Step 1: To resolve this issue, use the Convert function in SQL Server to convert the character string to a datetime value. The correct syntax is: Convert(datetime, '21-02-2012 6:10:00 PM', 120).
  2. Step 2: Replace '21-02-2012 6:10:00 PM' with your actual date and time value in the format 'YYYY-MM-DD HH:MM:SS AM/PM'.
  3. Step 3: Apply this conversion to both date1 and date2 columns when inserting data.

Using the Format Function

  1. Step 1: Alternatively, you can use the Format function in SQL Server to convert the character string to a datetime value. The correct syntax is: Format( Convert(datetime, '21-02-2012 6:10:00 PM', 120), 'yyyy-MM-dd HH:mm:ss tt').
  2. Step 2: Replace '21-02-2012 6:10:00 PM' with your actual date and time value in the format 'YYYY-MM-DD HH:MM:SS AM/PM'.
  3. Step 3: Apply this conversion to both date1 and date2 columns when inserting data.

✨ Wrapping Up

By following these steps, you should be able to successfully create your table and insert data using character strings. Remember to use the Convert or Format function to convert your character string to a datetime value before inserting it into the datetime column.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions