Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle/use special characters like percent (%) and ampersand (&) in Oracle SQL queries

I need to include the special character "%" in my LIKE clause in a SQL query, e.g.:

Select * From Some_Table Where Field_Name Like 'bla%bla&2';

How do I write that?

like image 514
W. Elbashier Avatar asked Aug 01 '17 20:08

W. Elbashier


1 Answers

If you want to match Field_Name values that contain 'bla%bla&2', then you need to write this:

set define off
Select * From Some_Table Where Field_Name Like '%bla\%bla&2%' escape '\';

You get to specify which character you want to use to escape a following character (thanks should go to mathguy, not me). You also have to set define off to prevent sqlplus from trying to substitute values in a string.

If, however, you want to match Field_Name values that exactly equal the given string, then you do this instead:

set define off
Select * From Some_Table Where Field_Name = 'bla%bla&2';
like image 130
Jeff Holt Avatar answered Oct 18 '22 15:10

Jeff Holt