How to Resolve: Bulk load data conversion error (type mismatch or invalid character for the specified codepage)…

This post helps you how to resolve the BULK INSERT-related error in SQL Server: Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row… , column … 

 

Reproducing the issue with an example

Consider an example where you have a text file which you want to import in SQL Server using BULK INSERT:

Resolving the error Bulk load data conversion error (type mismatch or invalid character for the specified codepage)...

 

Then, let’s say you created the corresponding table in order to import the data:

CREATE TABLE SampleDB.dbo.TestTable
(
id int,
code varchar(50),
descr varchar(50)
);
GO

 

Next, you run the below T-SQL statement in order to import the data in the above table:

BULK INSERT SampleDB.dbo.TestTable
   FROM 'c:\pathToFile.txt'  
   WITH   
      (  
         FIELDTERMINATOR =',',  
         ROWTERMINATOR ='\n'  
      );
GO

 

Right after you run the above T-SQL script, you get the below error message:

Msg 4864, Level 16, State 1, Line 9
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (id).

 

The answer can be obvious, but also you might need some time to figure it out. The issue in the above example is that you are trying to import the column headers as data!

 


Learn how to efficiently import and export data in SQL Server! Get the course today!

Check my online course titled “How to Import and Export Data in SQL Server” (special limited-time discount included in link).

Learn how to import and export data in SQL Server, via all the available built-in tools such as SSIS, BCP and more!

How to Import and Export Data in SQL Server - Online Course
Learn more about the course.

Enroll Now with Discount!


How to resolve the issue

The issue can be easily resolved by including the “FIRSTROW=2” option in your T-SQL code:

BULK INSERT SampleDB.dbo.TestTable
   FROM 'c:\pathToFile.txt'  
   WITH   
      (  FIRSTROW = 2,
         FIELDTERMINATOR =',',  
         ROWTERMINATOR ='\n'  
      );
GO

 

Featured Online Courses:

 

 

Read Also

Feel free to check our other relevant articles on SQL Server troubleshooting:

 

Featured Database Productivity Tools

Snippets Generator: Create and modify T-SQL snippets for use in SQL Management Studio, fast, easy and efficiently.

Snippets Generator - SQL Snippets Creation Tool

Learn more

 

Dynamic SQL Generator: Convert static T-SQL code to dynamic and vice versa, easily and fast.

Dynamic SQL Generator: Easily convert static SQL Server T-SQL scripts to dynamic and vice versa.

Learn more

 

Subscribe to our newsletter and stay up to date!

Check out our latest software releases!

Check out Artemakis’s eBooks!

 

Rate this article: 1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)

Loading...

Reference: SQLNetHub.com (https://www.sqlnethub.com)

© SQLNetHub