I have a CSV file which has French Characters in some of the fields.
But when I import this data into a DB. I do not see French characters instead it shows some other special characters.
Query I am using to import the .csv file is as follows:
--Create Table
Create Table A_test (A_1 VARCHAR(100))
--Bulk Import .csv file with ANSI encoding
BULK INSERT A_Test
FROM 'C:\A_Test.csv'
WITH
( DataFileType = 'widechar',
ROWTERMINATOR ='\n'
);
--Sample Data in C:\A_Test.csv file
Le vieux château
Une fête
Le dîner
L'hôtel
Could anyone help me on this?
You can alter the collation of the affected columns by running the following code (I just made up the column name and datatype):
ALTER TABLE dbo.a_test
ALTER COLUMN somecolumn varchar(100) COLLATE French_CI_AS NOT NULL;
Also you could create the original table with the relevant columns pre-collated:
CREATE TABLE dbo.a_test
(
[somecolumn] varchar(100) COLLATE French_CI_AS NOT NULL
)
BULK INSERT like this:
BULK INSERT a_test from 'C:\etc.txt' WITH (DATAFILETYPE = 'widechar')
Related
I have a .txt file which is 6.00 GB. It is a tab-delimited file so when I try to load it into SQL Server, the column delimiter is tab.
I need to load that .txt file into the database, but I don't need all the rows from the 6.00 Gb file. I need to be able to use a condition like
select *
into <my table>
where column5 in ('ab, 'cd')
but this is a text file and am not able to load it into db with that condition.
Can anyone help me with this?
Have you tried with BULK INSERT command? Take a look at this solution:
--Create temporary table
CREATE TABLE #BulkTemporary
(
Id int,
Value varchar(10)
)
--BULK INSERT has no WHERE clause
BULK INSERT #BulkTemporary FROM 'D:\Temp\File.txt'
WITH (FIELDTERMINATOR = '\t', ROWTERMINATOR = '\n')
--Filter results
SELECT * INTO MyTable FROM #BulkTemporary WHERE Value IN ('Row2', 'Row3')
--Drop temporary table
DROP TABLE #BulkTemporary
Hope this helps.
Just do a Bulk Insert into a staging table and form there move the data you actually want into a production table. The Where Clause is for doing something based on a specific condition inside SQL Server, not for loading data into SQL Server.
My SQL Server 2012 table is CUST_TABLE. It already has a lot of customer records (more than 10,000).
I have a CSV with the first column customer number which is my primary key. The second column has email addresses. The first row of this CSV contains columns heading custnum and email. The CSV has 125 data rows. I am using SSMS and want to update just 125 customer records and change their email.
The only solution I found was to have update statements to change the data. Is there any other easier way to do this? Like using the import data function by right-clicking on the database and then hovering over tasks. Thank you.
Read the csv into a temp table, then update your table using the temp table.
For example:
USE yourdb;
GO
IF OBJECT_ID('tempdb.dbo.#tmp', 'U') IS NOT NULL
DROP TABLE #tmp;
GO
CREATE TABLE #tmp (
t_cust_nr NVARCHAR(MAX),
t_email NVARCHAR(MAX)
)
SET NOCOUNT ON;
-- Read the csv, skip the first row
BULK INSERT #tmp
FROM 'C:\path\to\your.csv'
WITH (FIRSTROW = 2, FIELDTERMINATOR = ',', ROWTERMINATOR ='\n');
-- Trim whitespace
UPDATE #tmp
SET t_cust_nr = LTRIM(RTRIM(t_cust_nr)),
t_email = LTRIM(RTRIM(t_email));
-- Add your update statement here...
-- You also might have to cast the t_cust_nr to a diff. data type if needed.
SET NOCOUNT OFF;
DROP TABLE #tmp;
I have a .txt file which is 6.00 GB. It is a tab-delimited file so when I try to load it into SQL Server, the column delimiter is tab.
I need to load that .txt file into the database, but I don't need all the rows from the 6.00 Gb file. I need to be able to use a condition like
select *
into <my table>
where column5 in ('ab, 'cd')
but this is a text file and am not able to load it into db with that condition.
Can anyone help me with this?
Have you tried with BULK INSERT command? Take a look at this solution:
--Create temporary table
CREATE TABLE #BulkTemporary
(
Id int,
Value varchar(10)
)
--BULK INSERT has no WHERE clause
BULK INSERT #BulkTemporary FROM 'D:\Temp\File.txt'
WITH (FIELDTERMINATOR = '\t', ROWTERMINATOR = '\n')
--Filter results
SELECT * INTO MyTable FROM #BulkTemporary WHERE Value IN ('Row2', 'Row3')
--Drop temporary table
DROP TABLE #BulkTemporary
Hope this helps.
Just do a Bulk Insert into a staging table and form there move the data you actually want into a production table. The Where Clause is for doing something based on a specific condition inside SQL Server, not for loading data into SQL Server.
When I import a CSV or text file and bulk insert it into my database, the process successfully adds all record to the table.
My problem is that the inserted string is in Arabic, which appears as symbols in my database table. How can i solve this problem?
Insert using query
You need to choose an Arabic collation for your varchar/char columns or use Unicode (nchar/nvarchar).
CREATE TABLE MyTable
(
MyArabicColumn VARCHAR(100) COLLATE Arabic_CI_AI_KS_WS,
MyNVarCharColumn NVARCHAR(100)
)
Both columns should work.
Bulk Insert from file
This article explains how to bulk insert unicode characters.
Test Table
USE AdventureWorks2012;
GO
CREATE TABLE myTestUniCharData (
Col1 smallint,
Col2 nvarchar(50),
Col3 nvarchar(50)
);
Bulk Insert
DATAFILETYPE='widechar' allows the use of Unicode character format when bulk importing data.
USE AdventureWorks2012;
GO
BULK INSERT myTestUniCharData
FROM 'C:\myTestUniCharData-w.Dat'
WITH (
DATAFILETYPE='widechar',
FIELDTERMINATOR=','
);
GO
SELECT Col1,Col2,Col3 FROM myTestUniCharData;
GO
If I do this, it works correctly with plain old varchar, so I don't think it's a nVarchar problem:
declare #RAM TABLE(
Descr Varchar(128)
)
INSERT INTO #RAM(Descr) VALUES('De La Crème')
SELECT * FROM #RAM
But I'm having trouble importing the same data with a Bulk Insert.
Q: Is there a setting that I'm missing to allow accent grave?
Your question connected not with Bulk Insert (actually, there is no Bulk Insert in your code) or INSERT, but with data types. You need to use nvarchar and N in front of the string constant which define it as a UNICODE value.
declare #RAM TABLE(
Descr nvarchar(128)
)
INSERT INTO #RAM(Descr) VALUES(N'De La Crème')
SELECT * FROM #RAM