BULK INSERT tab delimited data with missing value - sql

I am trying to import a comma delimited data which is as follows.
5595,M,45,ABIQUIU,NEW MEXICO,132,EspanolaNM,40,AlbuquerqueHCS,.,NM,"ABIQUIU ,NM",324,1,1,0.1396
bulk insert Albuqurque from 'C:\AlbqurC.txt'
with
(
fieldterminator=',',
rowterminator='\n',
)
I am getting an error because there is a missing value "." after AlbuquerqueHCS
If I delete the missing value and replace that with a blank
5595,M,45,ABIQUIU,NEW MEXICO,132,EspanolaNM,40,AlbuquerqueHCS, ,NM,"ABIQUIU ,NM",324,1,1,0.1396
Import works fine,
Is there an option in sql BULK INSERT that will take care of missing values( "." ).... ?
Thanks in advance.

Related

Need to insert special character in string as insert script

I wanted to insert records having special character in snowflake.
Having record in source table :
order/date=2022-02-18/hour=12/85b3e2d8-0195-4238-b246-7ed6564ac464.json
I need to extract hour value i.e 12
I am able to extract the value using : cast(replace(substr(METADATA$FILENAME,28,2),'/','') as number)
But I need to create the insert script , I had tried :
'cast(replace(substr(METADATA$FILENAME,28,2),'/,'') as number)'
But getting error : FAILED CODE: 0 STATE: 22018 MESSAGE: Numeric value '5/' is not recognized
I tested your string in select and insert command as below:
select cast(replace(substr('order/date=2022-02-18/hour=12/85b3e2d8-0195-4238-b246-7ed6564ac464.json',28,2),'/','') as integer);
create table t1(c1 number);
insert into t1(c1) select cast(replace(substr('order/date=2022-02-18/hour=12/85b3e2d8-0195-4238-b246-7ed6564ac464.json',28,2),'/','') as integer);
If your issue is different, then share the exact command that you are executing and that's failing.
I got the solution :
Solution Snap shot
I wanted to insert this whole statement as string , I was facing issue due to special characters : / and '' .
Used backslash to resolve it.

Special characters in bulk insert query

I need to pass this query :
BULK INSERT e-Alexie.ENTREPRISE\beulin-ma.Correspondance_RCU_PP
FROM '\\ST077283\C:\Users\P20M511\Documents\Test_RCU.csv';
but I get an error on the - character. I've tried with ^_-^_ but it didn't work
Any idea?

Remove quotation marks using bulk insert in SQL Server

I am trying to use bulk insert for a .txt file, which is separated using a comma, but a few columns also have a double quotes, because of which when bulk insert is used, some rows are not inserted properly.
Also, I have to use bulk insert and not import/export functionality since I am automating my process of inserting the values in the table.
Here is the sample data: test.txt
ID, Date, Phone, Name
1,12/31/2017,"7415236541","Name1"
2,12/31/2017,"8524123652","Name2"
3,12/31/2017,"9853214536","Name2"
I use the following code, but it does not help
BULK INSERT xImportTable
FROM 'C:\Files\CSV\test.csv'
WITH
( FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
But this code does not remove the double quotes.

unicode characters writing to CSV issue

I have this table in a database.
create table #temp
(
name nvarchar(max)
)
insert into #temp
(
name
)
values
('ปภวรินทร์ เฉื่อยไธสง')
select * from #temp
When I am seeing this data in the website. the data is displaying as
ชญา สวัสดิ์โยธ
But when I am exporting this data to csv it is displaying as
ปภวรินทร์ เฉื่อยไธสง
I want to export the data to csc from sqlserver in the same way that shows in WEB.
How can i do that ?
Thanks in advance.
Try to add N before inserting your values. You can insert UNICODE characters.
insert into #temp
(
name
)
values
(N'ชญา สวัสดิ์โยธ')
This is either (a) an issue with your UTF target (e.g. you are targeting UTF-32 on export rather than UTF-8 / 16) or (b) the db you are using requires a symbolic string for inserting these characters-- sort of like how "??!" is a trigraph for "|".

How to insert data from text file into my table?

I have text files which contain one word per line, and I would like to add this content to a column in my table, the column type is Varchar, how can I accomplish that?
You can treat your file as a special case of CSV - it's a CSV file with only one column.
See this article for how to bulk insert from a CSV file.
BULK
INSERT CSVTest
FROM 'c:\csvtest.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
You can also use import wizard provided in the management studio. You can check this link for your reference.