How to fix failed data type conversion error

Greg Johnson 21 Reputation points
2021-02-15T08:13:33.39+00:00

I am inserting multiple columns from one table into another with MS SQL. I imported the data from Excel into a staging table and now inserting it into another table (to get around the float data type problem with excel) I updated that data types in the staging table to match the data types in the final table.

Simplified version with only 1 column:

Insert into [MKB].[dbo].[Purchases]
(Product_Type_ID)
select
'Product_Type_ID'
from [MKB].[dbo].[test_import3]

Error message: Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'Product_Type_ID' to data type int.

I have double checked and the Product_Type_ID column in the test_import3 table is listed as Int. The values in this columns are all whole numbers. Is there a way to properly convert and commit changing the data type into int to allow the insert to complete?

Thank you for your assistance!

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,788 questions
SQL Server Migration Assistant
SQL Server Migration Assistant
A Microsoft tool designed to automate database migration to SQL Server from Access, DB2, MySQL, Oracle, and SAP ASE.
532 questions
0 comments No comments
{count} votes

Accepted answer
  1. Olaf Helper 44,501 Reputation points
    2021-02-15T08:16:40.557+00:00

    Because of the apostrophe around "Product_Type_ID" in the SELECT part it's treaten as string, not as column name, and that string can't be converted to integer, that's why you get an error message. Remove them.

    =>

    Insert into [MKB].[dbo].[Purchases]
    (Product_Type_ID)
    select Product_Type_ID
    from [MKB].[dbo].[test_import3]
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.