Question

Am trying to CSV Data in notepad to my database,but am having conversion error. I got similar error message but different problem after a lot of hours Researching on the issue. error message:

Msg 4864, Level 16, State 1, Line 3
Bulk load data conversion error (type mismatch or invalid character for the specified  codepage) for row 1, column 4 (HireDate).
Msg 4832, Level 16, State 1, Line 3
Bulk load: An unexpected end of file was encountered in the data file.
Msg 7399, Level 16, State 1, Line 3
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did  not give any information about the error.
Msg 7330, Level 16, State 2, Line 3
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".    

File path:'c:\Student.txt'.
Sample content of Student.txt: 'wbrema','Milo','2012-02-15','2012-02-15'

db script:

CREATE TABLE [dbo].[Person](
[PersonID] [varchar](50) NOT NULL,
[FirstName] [varchar](50) NOT NULL,
[LastName] [varchar](50) NOT NULL,
[HireDate] [date] NOT NULL,
[Enrollment Date] [datetime] NOT NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED 
(
    [PersonID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
Was it helpful?

Solution

First: add value into you CSV for "PersonID" (or make it autoincrement).

Second: remove quotations from CVS. Your line should be:

100,wbrema,Milo,2012-02-15,2012-02-15

Now, using statement:

BULK
INSERT Person
FROM 'c:\Student.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

... you should be able to import data.

OTHER TIPS

Create a Holding Table for the raw data along the lines of

CREATE TABLE [dbo].[RAW_Person]
(
[PersonID] [varchar](50) NOT NULL,
[FirstName] [varchar](50) NOT NULL,
[LastName] [varchar](50) NOT NULL,
[HireDate] [varchar](50) NULL,
[Enrollment Date] [varchar](50) NULL
) 

BULK Insert the data into that table

And then perform the text to date conversion as part of the final Insert

INSERT INTO 
Person (PersonID, FirstName, LastName, HireDate, [Enrollment Date])
SELECT PersonID, FirstName, LastName, 
CONVERT(date,     [HireDate],        120), 
CONVERT(datetime, [Enrollment Date], 120)
FROM RAW_Person

I've used the formatting code 120 (ODBC data format) but you might need some other value look up the CONVERT function in the help files/MSDN for more information

Once you've successfully done that you can get rid of the holding table data

TRUNCATE TABLE [dbo].[RAW_Person]

Or if you're never doing this again

DROP TABLE [dbo].[RAW_Person]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top