0

This seems like it would be a question that has been answered a million times before, but searching for a bit only brought up replacing single quotes with double quotes.

The problem is that I have a file with a huge number of single quotes. Is there any way to insert a string as is, without the single quotes throwing an error. Any wrapper?

This is for pure SQL, I don't need to save this string from the backend or anything. I need a SQL command.

Edit: Just to clarify - the string I am trying to post is a document, in html format, with a large number of both double and single quotes, escaping each one individually would take enough time to warrant saving data in another format, which I would rather not do.

VSO
  • 11,546
  • 25
  • 99
  • 187

4 Answers4

4

If you have a file accessible to the SQL Server, you can read the contents in to a variable with OPENROWSET. For example, to read a Unicode text file at C:\drop\demo.html:

DECLARE @DocumentText nvarchar(MAX);

SELECT @DocumentText = BulkColumn
FROM OPENROWSET(BULK 'C:\drop\demo.html', SINGLE_NCLOB) file;

INSERT INTO Files (Column) VALUES (@DocumentText);
Mitch
  • 21,223
  • 6
  • 63
  • 86
2

Can you replace the single quotes with two single quotes? That is how SQL Server escapes the single quote in a string.

declare @test_string nvarchar(max) = 'string with a single '' quote in it';
print @test_string

string with a single ' quote in it
Sako73
  • 9,957
  • 13
  • 57
  • 75
  • Familiar with that solution. Want to see if there is something else without touching every single quote. – VSO Jul 15 '15 at 18:28
  • @Mitch: Yea, seems like what it will be in the end. It just seemed to make sense that there would be an "ignore all single quotes" command, somehow. – VSO Jul 15 '15 at 18:32
  • Nope. The string you are sending is either valid SQL (no embedded quotes), you are supposed to parameterize it. (Excepting for use of things like `OPENROWSET(BULK'filename', SINGLE_BLOB)`) – Mitch Jul 15 '15 at 18:34
  • How are you getting this string from the file into SQL? You can probably add a replace command on the reading of the file. – Sako73 Jul 15 '15 at 18:35
  • Read-only user agreement, so I just save it by hand on the rare occasions when it's updated. – VSO Jul 15 '15 at 18:37
1

You need to use double quotes:

select 'it''s a double quote'

Will return:

it's a double quote
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • Familiar with that solution. Want to see if there is something else without touching every single quote. – VSO Jul 15 '15 at 18:28
-1

Read the file, parse it, and do

    if(string.charAt(I))=="'"... charAt(I)="";
user3498796
  • 71
  • 1
  • 3