-1

I need to search in my column strings which contains '

select * from app_user au where au.last_name like '%'%'

but something like that gives me

SQL Error [42601]: Unterminated string literal started at position 55

I tried also '%\'%', but it's not working too, how to escape ' in postgres?

jjanes
  • 37,812
  • 5
  • 27
  • 34
mtmx
  • 827
  • 1
  • 10
  • 31
  • Does this answer your question? [Replace apostrophe in string](https://stackoverflow.com/questions/45758536/replace-apostrophe-in-string) [Insert text with single quotes in PostgreSQL](https://stackoverflow.com/questions/12316953/insert-text-with-single-quotes-in-postgresql) [Postgresql escape single quote in where clause](https://stackoverflow.com/questions/38700966/postgresql-escape-single-quote-in-where-clause) – Bergi Jul 03 '23 at 10:55

2 Answers2

0

You need to double the single quote to get the result

CREATE TABLE app_user (last_name text)
INSERT INTO app_user VALUES ('ma''affee')
select * from app_user au where au.last_name like '%''%'
last_name
ma'affee

fiddle

As you see in the sample the INSERT needs also a double single quote, but prepare statements with parameters will take the need to double single quote away when inserting or selecting

nbk
  • 45,398
  • 8
  • 30
  • 47
0

To escape ', you need to add it twice. Use '' to resolve it.

select * from app_user au where au.last_name like '%''%'
Mukul Bindal
  • 351
  • 1
  • 14