Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one escape an apostrophe in db2 sql

Tags:

sql

escaping

db2

I'm looking for the db2 equivalent of T-SQL's:

INSERT INTO People (Surname) VALUES ('O''Hara'); 
like image 265
grenade Avatar asked Mar 14 '10 13:03

grenade


People also ask

How do you escape special characters in DB2?

To search for a special character that has a special function in the query syntax, you must escape the special character by adding a backslash before it, for example: To search for the string "where?", escape the question mark as follows: "where\?"

How do you handle an apostrophe in a SQL 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. With a single quote this is typically accomplished by doubling your quote.

How do I escape a special character in SQL query?

Use braces to escape a string of characters or symbols. Everything within a set of braces in considered part of the escape sequence. When you use braces to escape a single character, the escaped character becomes a separate token in the query. Use the backslash character to escape a single character or symbol.

How do I insert a special character in DB2?

There are no "special characters" in SQL databases (including DB2), unless they are special to the application doing the inserting. If you are using parameterized statements: INSERT INTO mytable (mycol) VALUES (?) , anything goes.


2 Answers

Use two apostrophes '' to get a single apostrophe on DB2 too, according to the DB2 Survival Guide. Isn't that working for you?

like image 189
brabster Avatar answered Sep 19 '22 21:09

brabster


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') `

like image 25
Rakesh Juyal Avatar answered Sep 19 '22 21:09

Rakesh Juyal