Mani Ratna
Mani Ratna

Reputation: 911

How to search the name starting with special character in postgres

I have names which starts with special characters.I want to list those names. Some of the names are '%faf yf? -s_', '%faO{','%]fac?)f' and so on.

My query to list the name starting from '%' is like this:

Select * from table where name like '%fa%';

This query is not working and it is also listing the names which starts from 'fa'. I only need the values which start from '%fa'. I also tried the query like this

Select * from table where name like'[%]fa%'. 

Upvotes: 0

Views: 1190

Answers (1)

user330315
user330315

Reputation:

You need to escape the %:

Quote from the manual

To match a literal underscore or percent sign without matching other characters, the respective character in pattern must be preceded by the escape character. The default escape character is the backslash but a different one can be selected by using the ESCAPE clause. To match the escape character itself, write two escape characters

Select * 
from the_table 
where name like'\%fa%' escape '\' 

Upvotes: 1

Related Questions