Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underscore is not working in oracle like clause

When development, I used 'test_1%' to find 'test_123' in like. But in production environment its not working. Using 'escape '\'' is working. is there any setting needs to set in oracle? I want to use without escape '\''.

like image 294
Ramesh Avatar asked Jan 27 '14 11:01

Ramesh


People also ask

How do you underscore in like Operator?

The SQL LIKE Operator The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single 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 (_).

How do I match an underscore in SQL?

If you are searching for an underscore then escape it with a '\' so you would search for' \_'. When matching SQL patterns, you can use these symbols as placeholders: % (percent) to substitute for zero or more characters, or _ (underscore) to substitute for one single character.

IS LIKE operator case sensitive in Oracle?

The default behaviour of LIKE and the other comparison operators, = etc is case-sensitive.


3 Answers

try this in SQL Developer:

SELECT * FROM TABLE1 WHERE NAME LIKE 'test\_1%' escape '\' 

in sql plus:

set escape '\' SELECT * FROM TABLE1 WHERE NAME LIKE 'test\_1%'; 
like image 78
Hamidreza Avatar answered Nov 20 '22 18:11

Hamidreza


The other answers using the ESCAPE '\' didn't work for me, but I was able to overcome this issue by using a REPLACE function:

SELECT * FROM name_of_table WHERE REPLACE(description, '_', '~') LIKE 'testing~%';
like image 42
laughsloudly Avatar answered Nov 20 '22 18:11

laughsloudly


In Oracle, you can also use ESCAPE like this:

SELECT * FROM name_of_table WHERE description LIKE 'testing\_%' ESCAPE '\';
like image 45
Sohail xIN3N Avatar answered Nov 20 '22 18:11

Sohail xIN3N