Special characters displaying incorrectly after BULK INSERT - sql

I'm using BULK INSERT to import a CSV file. One of the columns in the CSV file contains some values that contain fractions (e.g. 1m½f).
I don't need to do any mathematical operations on the fractions, as the values will just be used for display purposes, so I have set the column as nvarchar. The BULK INSERT works but when I view the records within SQL the fraction has been replaced with a cent symbol (¢) so the displayed text is 1m¢f.
I'm interested to understand why this is happening and any thoughts on how to resolve the issue. The BULK INSERT command is:
BULK INSERT dbo.temp FROM 'C:\Temp\file.csv'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n' );

You need to BULK INSERT using the CODEPAGE = 'ACP', which converts string data from Windows codepage 1252 to SQL Server codepage.
BULK INSERT dbo.temp FROM 'C:\Temp\file.csv'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n', CODEPAGE = 'ACP');
If you are bringing in UTF-8 data on a new enough version of SQL Server:
[...] , CODEPAGE = '65001');
You may also need to specify DATAFILETYPE = 'char|native|widechar|widenative'.

Related

How to insert null value when datatype error is fired using Bulk Insert?

I'm using Bulk Insert command in T-SQL cause I have a too big csv file. This file has fields that use numeric datatypes like float, but in these fields sometimes there are data with strings like "S/N".
So, how can I force that fields to NULL when I get an string value while I'm inserting?
Now I'm using this command, can you help me?
BULK INSERT [CUBOS_FINDIRECT].[dbo].[ListadoRobinsonTXT]
FROM '\\10.0.20.17\d$\listadoRobinson\listadoRobinsonDef.csv'
WITH
(
FIRSTROW = 2,
MAXERRORS = 0,
FIELDTERMINATOR = ';',
ROWTERMINATOR = '\n'
)
Thank you in advance so much.

SQL; csv import with semicolons in data and double quotes

I'm wanting to import a CSV file which has some values as such:
123;456;"78;9";1011
Simply said, there are some quotes in a value, but the value is within double quotes. When I use a bulk import, the value '"78' is put into one column, whereas '9"' is put into the next column. How can I prevent this?
I am using below query:
BULK INSERT CSVTest
FROM 'c:\csvtest.csv'
WITH
(
FIELDTERMINATOR = ';',
ROWTERMINATOR = '\n'
)
GO
I'm using SQL Server!
In a test environment i've setup the new sql server, and the fieldquote seems to be ignored in the statement, and the fields are still split up. What am I doing wrong? I'm doing:
BULK INSERT CSVTest
FROM 'c:\csvtest.csv'
WITH
(
FIELDTERMINATOR = ';',
ROWTERMINATOR = '\n',
FIELDQUOTE='"'
)
GO

Bulk Insert with special characters as delimiters

I am trying to bulk insert using special characters as the field terminators.
My input file looks like this;
ASCII: 254 is a field quote and ASCII:020 is a fieldterminator.
SQL Server version is 11.0.5058.0
My code so far;
bulk
INSERT edMe.dbo.test
FROM 'H:\test.dat' WITH (fieldterminator = 'þþ',
rowterminator = '\n',
datafiletype = 'widechar',
codepage ='acp'
)
However, 0 records get inserted;
Bulk load: DataFileType was incorrectly specified as widechar. DataFileType will be assumed to be char because the data file does not have a Unicode signature.
Bulk load: DataFileType was incorrectly specified as widechar. DataFileType will be assumed to be char because the data file does not have a Unicode signature.
(0 row(s) affected)
Thanks

Bulk insert from txt in SQL table

I need to do some bulk inserts in SQL Table from a txt file.
bulk insert [dbo].[TempSample]
from 'D:\sqls\sample.txt'
with (fieldterminator = ',', rowterminator = '\n')
go
In the txt file I have descriptions like 'Hörsching'. After insert is made I found descriptions in my table like 'H÷rsching'. How can we deal with that ? The collation of the table is set to Latin1_General_CI_AS.
How is the file encoded?
Have you tried using the CODEPAGE parameter to specify the file's encoding?

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.