skip to Main Content

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?

2

Answers


  1. 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

    Login or Signup to reply.
  2. To escape ', you need to add it twice. Use '' to resolve it.

    select * from app_user au where au.last_name like '%''%'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search