24

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

Ramesh
  • 607
  • 1
  • 9
  • 22

5 Answers5

37

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
  • 2,938
  • 1
  • 16
  • 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
  • 1
    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." – osullic Nov 20 '20 at 12:28
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
  • 555
  • 6
  • 12
4

In Oracle, you can also use ESCAPE like this:

SELECT * FROM name_of_table WHERE description LIKE 'testing\_%' ESCAPE '\';
Sohail xIN3N
  • 2,851
  • 2
  • 27
  • 29
1

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

select * from all_tables where  table_name  not like '%^_%' escape '^' ;
Ian Campbell
  • 21,281
  • 13
  • 26
  • 51
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
  • 113
  • 1
  • 5