How to import data with double quotes in SQL Server 2005
-
13-12-2019 - |
Question
I have to import a text file with data which contains double quotes. Actually I have created a text file which contains aix commands and their explanation.
Now the commands contains double quotes. So using the regular approach of importing bulk data into SQL Server 2005 I am getting error.
My text file is like:
1,acledit "file",for changing the ACL(access control list) of a file
2,anacheck -ap,error analysing for Escala E + T
3,bsh,starts a born shell
4,cancel "print job number",for killing a print job
5,chgrp "group" "file",changes the group of a file
6,chitab "inittab statement",for disabling a statement of the /etc/inittab
7,chmod 0777 "file",gives everybody the right to read, write and execute the specified file
8,chown "user" "file",changes the owner of a file
9,compress -v "file",compresses a file
10,cplv,copies a logical volume (no mirroring)
Can someone plz guide me how to import this data as it is in SQL Server 2005?
Solution
Use BULK INSERT
with the FIELDTERMINATOR
option
bulk insert [yourtablename] from 'yourfilepath' with (fieldterminator = ',')
OTHER TIPS
You can use a function Scalar Valued function like below to encode it to some other character and store it and decode it when you retrieve.
CREATE FUNCTION EncodeString
(@data as varchar(max))
RETURNS varchar(max)AS
BEGIN
DECLARE @result as varchar (max)
SELECT @result = REPLACE(@data , '"','_')
RETURN @result
END
The above function returns 1,acledit _file_,for changing the ACL(access control list) of a file
for your input number 1. you can use any other ASCII/unicode instead of _
, additionally you need to write decode function to use in retrieval and searching functioanlity. Hope this shall help.