LIKE clause with \ in PostgreSQL

I have this behavior in PostgreSQL 9.3:

-- (1) this "doesn't" work
select 't\om' like '%t\om%'
-- result = false

-- (2) this works
select 't/om' like '%t/om%'
-- result = true

      

Why is query result (1) false? What is the best way to get truth in (1) query?

+3


source to share


2 answers


\

doesn't really matter in SQL other than inside a clause for a statement LIKE

, where it can be used to escape wildcard characters.

But you can define another escape character for LIKE, which then makes the character \

"normal":

select 't\om' like '%t\om%' escape '#';

      



change

As Sunrelax commented, you can also use an empty string as an "escape" sequence:

select 't\om' like '%t\om%' escape '';

      

+4


source


\

is an escape sequence, so you need to escape it too:

select 't\om' like '%t\\om%';

      



There is also a configuration option that you can set. See Escaping backslashes in Postgresql

+1


source







All Articles