Underscore is not working in oracle like clause
Asked Answered
L

5

27

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 '\''.

Lelia answered 27/1, 2014 at 11:57 Comment(4)
it is because of that underscore works same as percent in like clause but it refers to just one character.Garner
I want to search value with underscore without using escape. How to do?Lelia
You have the same string "test_123" in your prod environment, and your query is not able to find it?Fluidize
_ matches any character including _, so test_123 will be found :)Celibacy
G
48

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%';
Garner answered 27/1, 2014 at 12:26 Comment(3)
How can this be done in a PLSQL package. When I submit the package for compilation the escape '\' gives me a ORA-00933: SQL command not properly endedSeedy
That ESCAPE clause is easy to miss; this answer could do with pointing it out more.Christinachristine
I wanted to point out something from the Oracle docs that I think is important to know... "If esc_char is not specified, then there is no default escape character."Lillielilliputian
M
5

In Oracle, you can also use ESCAPE like this:

SELECT * FROM name_of_table WHERE description LIKE 'testing\_%' ESCAPE '\';
Marillin answered 10/5, 2018 at 6:35 Comment(0)
J
5

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~%';
Jeaniejeanine answered 13/8, 2019 at 23:11 Comment(0)
S
1

For me this has worked (using ^ as escape character):

select * from all_tables where  table_name  not like '%^_%' escape '^' ;
St answered 24/9, 2020 at 11:35 Comment(0)
B
0

Using _ as escape character and using double _ in like condition has worked for me.

If you want to add more condition, put ESCAPE keyword after LIKE condition.

SELECT * FROM USER_TABLES WHERE TABLE_NAME LIKE '%__%' ESCAPE '_' AND ...
Burnight answered 26/7, 2021 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.