If I want to select strings with underscores using Oracle SQL Developer, how do I have to escape those?
I tried:
name like '%_%'
name like '%'_%'
name like '%\_%'
but none of this helped.
The escape character makes SQL treat whatever follows it as a literal character. Put square-brackets around the underscore in the Like statement.
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.
The ESCAPE clause identifies the backslash (\) as the escape character. In the pattern, the escape character precedes the underscore (_). This causes Oracle to interpret the underscore literally, rather than as a special pattern matching character.
Answer: Oracle handles special characters with the ESCAPE clause, and the most common ESCAPE is for the wildcard percent sign (%), and the underscore (_). For handling quotes within a character query, you must add two quotes for each one that is desired.
You need to use the explicit escape
; in this way you can decide a character to use for escaping and then use it in your LIKE
.
For example, here I use the '!'
to escape special characters:
select str
from (
select 'a_b' str from dual union all
select 'ab' from dual
)
where str like '%!_%' escape '!'
gives
STR
---
a_b
Oracle suggests the code below which workded for me:
SELECT last_name
FROM employees
WHERE last_name LIKE '%A\_B%' ESCAPE '\'
ORDER BY last_name;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With