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>')
Related
Hi,
I am trying to insert a value "['HCC111', 'HCC112']" in a redshift column but getting an error which says column "['HCC111', 'HCC112']" does not exist in the table. Is there a workaround for this ?
Query:
insert into #trhcc values('COMMUNITY V22', "['HCC111', 'HCC112']",'HCC10')
Here is the snapshot of the query that is submitted --
"['HCC111', 'HCC112']" is not a valid way because you used doube quotes. Text must use single quotes. Your problem is that you also use single quotes in your entry text. That is easily solved by escaping your single quotes. You do that by doubling the single quote like so:
'[''HCC111'', ''HCC112'']'
This question already has answers here:
Insert text with single quotes in PostgreSQL
(8 answers)
Correctly delimit single quotes in postgres insert/update
(1 answer)
Closed 4 years ago.
I'm writing a function that logs interactions with a Facebook messenger bot. Anything the user says is logged in a PostgreSQL database.
My insert statement looks like this:
INSERT INTO interactions (fbid,date,time,event) VALUES ('senderid','2018-10-
01','11:15:48','text')
Treat "senderid" as a 20-digit number and "text" as whatever the user says.
Now, the above statement works IF the text from the user contains no apostrophe characters. However, sometimes the text DOES contain apostrophes. In these cases, the insert doesn't work.
For example, if the user says "Let's chat" then my SQL looks like this:
INSERT INTO interactions (fbid,date,time,event) VALUES
('senderid','2018-10-01','11:15:19','Let's Chat!')
and I get the following error:
Query result: error: syntax error at or near "s"
I know this is because I would need to escape the apostrophe. I've tried working around this by using double quotes in my insert statement, like this:
INSERT INTO interactions (fbid,date,time,event) VALUES ("senderid","2018-09-
28","10:50:07","Let's chat")
But when I do this I get the following error:
ERROR: column "senderid" does not exist
So I have two questions:
CAN I use double quotes in a SQL insert?
If I CAN'T use double quotes in a SQL insert, how would I escape an apostrophe character, bearing in mind there may not always BE an apostrophe?
Try below: you need to use single quote twice in case of apostrophi like let's will be let''s
INSERT INTO interactions (fbid,date,time,event) VALUES ('senderid','2018-09-
28','10:50:07','Let''s chat')
1.CAN I use double quotes in a SQL insert?
no you can't because double quote means column name in postgrey that why when sql engine found that column when you use double quote
you can use
'Let''s Chat!'
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)="";
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.
I'm looking for the db2 equivalent of T-SQL's:
INSERT INTO People (Surname) VALUES ('O''Hara');
Use two apostrophes '' to get a single apostrophe on DB2 too, according to the DB2 Survival Guide. Isn't that working for you?
Brabster is correct. You are supposed to escape ' with ''
So to insert O'Hara , you will have to write O''Hara
Excerpt from:
http://www.michael-thomas.com/tech/db2/db2_survival_guide.htm
Escape character.
To insert a single quote, use 2 single
quotes ( '' ). To insert pet's use
the following pet''s.
Example: insert into MYTABLE (question,answer) values ('What is
your pet''s name?','blacky') `
just make it simple.
Keep your query in the single quotes ('). and where ever you are using qoutes, make it double.
for e.g. u want to insert ...... I'm fine. There's an answer.
in SQL we need to give ....... 'I''m fine. There''s an answer.'
I was trying to insert into DB2 a regex expression for an email address. I thought I had to escape all characters as outlined at the proceeding link, but escaping only the single quote contained within the regex seems to be all I needed to escape to get it properly inserted into the database:
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_10.1.0/com.ibm.db2.luw.admin.ts.doc/doc/c0059609.html