Bulk insert error, when loading date format - sql

I'm receiving a lot of .csv files that I need to bulk insert into a SQL table.
In the csv file the date format is in YYYY-MM-DD and a seperate column for time which is in format HH:MM:SS.
"2016-11-24";"01:00:16"
In my table I created the two columns as a date datatype and a time datatype and using this piece of code to insert:
BULK
INSERT [dbo].[table_name]
FROM 'filepath'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ';',
ROWTERMINATOR = '\n'
)
But I get this error:
Msg 4864, Level 16, State 1, Line 37
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 1 (date).
If I use a normal insert into statement it works...

First check your physical table date and time columns data type. It has datetime format, which means first you move all .csv file data to a temporary table with varchar datatype. After inserted in temporary table then move to physical table with use of datetime conversion.

Related

BulkInsert CSV file to table SQL Server

I've got some hard problems inserting my CSV file from a location into a table that will be used for making reports and data extraction matched with other data.
Create table #PD_ABC (
Column1
Column2 etc etc
)
BULK INSERT #PD_ABC FROM 'F:\BulkInsert\Andrej\UtkastAntal(23)Export20141003.csv'
WITH (FIELDTERMINATOR = ';',CODEPAGE = 'RAW',ROWTERMINATOR = '0x0a')
insert into Maintenance.dbo.PD_ABC_Del1
select * from #PD_ABC
So far I supose everything should work. I made a similar script for .txt files but when comming to CSV somehow I cannot import them correctly.
This is the erros message I've been receving.
Msg 4863, Level 16, State 1, Procedure PD_ABC_SP, Line 49
Bulk load data conversion error (truncation) for row 1, column 3 (Gldnr).
No idea how to move forward from this.
It looks like your Column3 doesn't have enough characters for data. Is column3 type char or varchar? If so, you should give it more characters.

Error on BULK insert date [duplicate]

This question already has an answer here:
Fun with BULK INSERT SQL SERVER - type mismatch or invalid character for the specified codepage
(1 answer)
Closed 9 years ago.
I am trying to BULK insert into sql server 2008 and i get an error on the Date column.
SET DATEFORMAT dmy
BULK
INSERT CustomSelection
FROM 'c:\test.csv'
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 1 (Date).
This is the Date in the .csv file: 18/08/2012.
Any ideas how to solve this ?
your date format is invalid thats way sql server giving error .
To solve these type of issue you have to follow following steps :
1. Import csv in database table , table column must be varchar it should not be datetime .
2. Then run following query to find invalid dates .
select inserteddate from table where isdate(inserteddate) = 0
3. then update those date manually or by update query something like :
UPDATE TABLE1
SET INSERTEDDATE = CONVERT(DATETIME , '18/08/2012' , 105)
WHERE INSERTEDDATE = '18/08/2012'

Alter sql Table data format

I have a Nvarchar column in a database table which contains date values. Date values are stored in two formats
2008-05-20
22/04/2011
Now I need to convert this column to a date column. When I'm try the following query:
set dateformat dmy
alter table tblDocumentRevision
alter column RevisionDate date
it returns an error:
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
The statement has been terminated.
Please help me to solve this
Update the date string in the table so they all have the same format (one that SQL server can cast from) before you change the type to DateTime.

Passing default values to a column in Bulk insert

I am trying to get data from a csv file with the following data.
Station code;Priority vehicle;DateBegin;DateEnd
01;y;20100214;20100214
02;n;20100214;20100214
03;;20100214;20100214
Now I want a value 'n' in the table when no data is provided for the column 'Priority vehicle' in csv file.
I am writing the query as
BULK INSERT dbo.#tmp_station_details
FROM 'C:\station.csv'
WITH (
FIELDTERMINATOR ='';'',
FIRSTROW = 2,
ROWTERMINATOR = ''\n''
)
Check the full explanation here:
http://msdn.microsoft.com/en-us/library/ms187887.aspx
"By default, when data is imported into a table, the bcp command and BULK INSERT statement observe any defaults that are defined for the columns in the table. For example, if there is a null field in a data file, the default value for the column is loaded instead. "
My suggestion is to specify a default value for the Priority vehicle column and the Null value from the csv file will be overwritten to your SQL table with the default value specified in the table design.

Inserting Dates with BULK INSERT

I have a CSV file, which contains three dates:
'2010-07-01','2010-08-05','2010-09-04'
When I try to bulk insert them...
BULK INSERT [dbo].[STUDY]
FROM 'StudyTable.csv'
WITH
(
MAXERRORS = 0,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
I get an error:
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (CREATED_ON).
So I'm assuming this is because I have an invalid date format. What is the correct format to use?
EDIT
CREATE TABLE [dbo].[STUDY]
(
[CREATED_ON] DATE,
[COMPLETED_ON] DATE,
[AUTHORIZED_ON] DATE,
}
You've got quotes (') around your dates. Remove those and it should work.
Does your data file have a header record? If it does, obviously your table names will not be the correct data type, and will fail when SQL Server tries to INSERT them into your table. Try this:
BULK INSERT [dbo].[STUDY]
FROM 'StudyTable.csv'
WITH
(
MAXERRORS = 0,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
FIRSTROW = 2
)
According to MSDN the BULK INSERT operation technically doesn't support skipping header records in the CSV file. You can either remove the header record or try the above. I don't have SQL Server in front of me at the moment, so I have not confirmed this works. YMMV.