Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape underscores in Oracle SQL Developer?

Tags:

sql

oracle

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.

like image 929
RedSea Avatar asked Mar 22 '17 08:03

RedSea


People also ask

How do I escape an underscore in SQL?

The escape character makes SQL treat whatever follows it as a literal character. Put square-brackets around the underscore in the Like statement.

How do I escape special characters in SQL Developer?

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 escape an Oracle character?

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.

Is underscore a special character in Oracle?

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.


2 Answers

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
like image 163
Aleksej Avatar answered Oct 15 '22 19:10

Aleksej


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;
like image 31
skourkos Avatar answered Oct 15 '22 17:10

skourkos