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:
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! Enroll to the course!
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 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:
- Boost SQL Server Database Performance with In-Memory OLTP
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners: Windows Forms (C#)
- Introduction to Data Science and SQL Server Machine Learning
- Introduction to Azure SQL Database
- SQL Server 2019: What’s New
- Entity Framework: Getting Started (Ultimate Beginners Guide)
- How to Import and Export Data in SQL Server
- Get Started with SQL Server in 30 Minutes
- A Guide on How to Start and Monetize a Successful Blog
Feel free to check our other relevant articles on SQL Server troubleshooting:
- How to Resolve: Cannot Connect to WMI Provider (SQL Server Configuration Manager)
- Error converting data type varchar to numeric
- Error converting data type varchar to float
- There is not enough space on the disk. (mscorlib)
- A network-related or instance-specific error occurred while establishing a connection to SQL Server
- SQLServerAgent could not be started (reason: Unable to connect to server ‘(local)’; SQLServerAgent cannot start)
- ORDER BY items must appear in the select list if SELECT DISTINCT is specified
- There is no SQL Server Failover Cluster Available to Join
- There is insufficient system memory in resource pool ‘internal’ to run this query.
- The SELECT permission was denied on the object ‘extended_properties’, database ‘mssqlsystemresource’, schema ‘sys’.
- … all SQL Server troubleshooting articles
Featured Database Productivity Tools
Snippets Generator: Create and modify T-SQL snippets for use in SQL Management Studio, fast, easy and efficiently.
Dynamic SQL Generator: Convert static T-SQL code to dynamic and vice versa, easily and fast.
Subscribe to our newsletter and stay up to date!
Check out our latest software releases!
Check our eBooks!
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
Artemakis Artemiou is a Senior SQL Server Architect, Author, a 9 Times Microsoft Data Platform MVP (2009-2018) and a Udemy Instructor. He has over 15 years of experience in the IT industry in various roles. Artemakis is the founder of SQLNetHub and TechHowTos.com. Artemakis is the creator of the well-known software tools Snippets Generator and DBA Security Advisor. Also, he is the author of many eBooks on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Moreover, Artemakis teaches on Udemy, you can check his courses here.