How to Post a SQL String With Quotes? - sql

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.

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);

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

You need to use double quotes:
select 'it''s a double quote'
Will return:
it's a double quote

Read the file, parse it, and do
if(string.charAt(I))=="'"... charAt(I)="";

Related

Is it possible to specify the CSV quote character when exporting BQ tables?

I am exporting tables using the following command bq extract --destination_format=CSV --compression=GZIP --noprint_header <table> <gcs>
I noticed that strings containing double quotes had double quotes appended upon export, and stumbled upon the reason in this thread: Data between quotes and field separator
It seems like there was a way to change the quote character (per Jordan's comment in the answer) at one point in the past, but I cannot seem to find it today.
Is it currently possible to specify the quote character when exporting BQ tables as CSV?
I think Jordan's comment was related to Load Jobs and not applicable for Extract Jobs
The option to try I see in your case is to replace all double quotes with single quotes (via SELECT REPLACE(field,...) ... FROM table ... with new table as destination) and then do extract for that modified table
Of course this assumes you can afford such substitution and it will not than break your product/app.
Below is simplified example of query you should run
#standardSQL
SELECT * REPLACE (REPLACE(fieldWithIssue, '"', "'") AS fieldWithIssue)
FROM yourTable
First REPLACE is part of SELECT * REPLACE feature
Whereas second REPLACE is just simple string function that replaces double quotes with single quotes

problems with quotes in SQL Netezza

Inside a procedure I have a variable that contains SQL for example like this:
examplesql:= 'SELECT 'asd';';
Where the SQL itself is enclosed with single '' and a value further inside the SQL is also enclosed with single ''. I tried using double "" marks instead but it didnt work. Any suggestions how to accomplish this in Netezza ?
The variable examplesql is later called with EXECUTE IMMIDIATE
This should be the same as (almost) every other DBMS, you need to double the single quotes within the string:
examplesql:= 'SELECT ''O''''Hara';

Insert query for HTML string containing single and double quotes

I have Sql database with table name QUESTION
I want to insert value for QuestionTitle as html string contains single and double quots.
Insert query is
INSERT INTO QUESTION (QuestionType,QuestionID,QuestionTitle)
VALUES("MRQ","QNB5T6TKDMS",'<h1>What are types of special symbols
like #.~!#!$%^&*()_['"+-=</h1>')
When I try to execute this query in Sql it gives an error ,unrecognized token: ""+-=')" .
I try for \' and \'' ,still not work.
How can I do this so that it will work for html string.
The apostrophe, or single quote, is a special character in SQL that specifies the beginning and end of string data. This means that to use it as part of your literal string data you need to escape the special character.
The first one works fine for MySQL
INSERT INTO QUESTION (QuestionType,QuestionID,QuestionTitle) VALUES(8,"QNB5T6TKDMS",'<h1>What are types of special symbolslike #.~!#!$%^&*()_[\'"+-=</h1>')
or
This will work in SQLite
INSERT INTO QUESTION (QuestionType,QuestionID,QuestionTitle) VALUES(8,"QNB5T6TKDMS",'<h1>What are types of special symbolslike #.~!#!$%^&*()_[''"+-=</h1>')

Quotes in a long multi-variable SQL request in Basic4android

All B2A users.
Again about double quotes in SQL: how to be (replace quotes) in B2A long SQL INSERT\UPDATE request string, if this string is created dinamically, fields names and qty are variables (download by HTTP), and any value of them can be a string with quotes ?
Fields values are also in double quotes for INSERT, and I mean that whole the big SQL string cannot be easy formatted as string with & QUOTE & parts.
B4A gives SQLite exception "sintax error". Quotes in the values must be saved\used.... :-( not to be replaced by two single quotes.
What more symbols in the fields values must be specially checked (excepting destructive SQL injections...)?
The solution is to use parameterized queries with ExecQuery2 or ExecNonQuery2. SQL docs.
You can also use DBUtils.

Special character in varchar in SQL

I am inserting text from a file into a table, few of the lines have words like "you'll" or "don't". When I insert these lines as varchar in my table, I get an error saying - near "ll": syntax error. How do I overcome this?
Your single quote is being considered as the end of your string. Escape the quote that exists within your string to avoid this problem.
You need to escape your SQL statement. If you are using SQL Server, then you can use QUOTENAME to resolve this.
Use two apostrophes within apostrophe-quoted strings to insert the apostrophe:
insert into footable (foo) values('you''ll')
Thank you all for responses, since I was using sqlite3, there are inbuilt string formating functions available with the library, so I was able to use sqlite3_mprintf with %q instead of %s and it took care of single quotes.