Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I escape multiple characters in an sql like string?

Tags:

sql

escaping

A single character can be escaped like this:

select * from tableName where columnName like 'CU_C' escape '_';

I need to escape multiple characters ("% " and "_"):

select * from tableName where columnName like 'C%U_C' escape ??;

How do I escape multiple characters?

like image 632
Dolphin Avatar asked Aug 07 '13 01:08

Dolphin


1 Answers

You misunderstood the meaning of escape: it lets you define a character such that when you put it in front of another character, that other character is interpreted literally, not as a meta-character. You need only one such escape character: you can use it to escape any meta-character.

In the example below I used '#' as my escape character:

select * from tableName where columnName like 'C#%U#_C' escape '#'

This tries to match C%U_C strings where both '%' and '_' are interpreted literally, not as a sequence of any characters or any single character.

like image 196
Sergey Kalinichenko Avatar answered Oct 12 '22 07:10

Sergey Kalinichenko