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 '\''.
Asked
Active
Viewed 4.2k times
26
-
1it is because of that underscore works same as percent in like clause but it refers to just one character. – Hamidreza Jan 27 '14 at 12:06
-
I want to search value with underscore without using escape. How to do? – Ramesh Jan 27 '14 at 12:08
-
You have the same string "test_123" in your prod environment, and your query is not able to find it? – Incognito Jan 27 '14 at 12:16
-
`_` matches any character including `_`, so `test_123` will be found :) – Egor Skriptunoff Jan 27 '14 at 12:26
5 Answers
44
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%';

Hamidreza
- 3,038
- 1
- 18
- 15
-
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 ended – Superdooperhero Jun 13 '17 at 11:37
-
That `ESCAPE` clause is easy to miss; this answer could do with pointing it out more. – jpmc26 Nov 30 '17 at 20:08
-
1I 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." – osullic Nov 20 '20 at 12:28
5
In Oracle, you can also use ESCAPE
like this:
SELECT * FROM name_of_table WHERE description LIKE 'testing\_%' ESCAPE '\';

Sohail xIN3N
- 2,951
- 2
- 30
- 29
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~%';

laughsloudly
- 604
- 7
- 12
1
For me this has worked (using ^ as escape character):
select * from all_tables where table_name not like '%^_%' escape '^' ;

Ian Campbell
- 23,484
- 14
- 36
- 57

Laszlo
- 11
- 1
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 ...

Çağlar Duman
- 133
- 2
- 6