Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping Question Mark (?) in SQL WHERE Clause

Tags:

sql

In the various SQL products I use, a question mark (?) marks an input parameter in a query.

How do I escape that behavior and search for text that contains a question mark, for example...

SELECT *
FROM   A_TABLE
WHERE  A_FIELD = 'This is a question mark not an input parameter?'

...where the question mark above is a literal question mark, not a place holder. If the answer is product specific, I am currently using Derby (aka Java DB).

like image 402
John Fitzpatrick Avatar asked Aug 29 '12 10:08

John Fitzpatrick


People also ask

What are the escape characters in SQL?

In ANSI SQL, the backslash character (\) is the escape character.

What is the use of '?' In SQL?

In these statements, you can use a question-mark ( ? ) placeholder where a parameter must be supplied when the statement is executed.

Can we use question mark in SQL query?

The question mark can appear where a host variable might appear if the statement string were a static SQL statement. If you want to run the same SELECT statement several times, using different values for LASTNAME, you can use an SQL statement that looks like this: SELECT WORKDEPT, PHONENO FROM CORPDATA.


2 Answers

You don't need to escape ? at all. It has no special meanings with the WHERE clause as in the case you posted.

like image 111
Mahmoud Gamal Avatar answered Sep 22 '22 07:09

Mahmoud Gamal


As mentioned previously, a '?' following an equals will not act as a wildcard. To search for any rows within a field that contain a question mark, use:

SELECT * FROM A_TABLE
WHERE A_FIELD LIKE '%[?]%'
like image 30
Charlie Avatar answered Sep 22 '22 07:09

Charlie