sqlpostgresqlstring-constant

PostgreSQL where clause condition check with value that contains single quote


Suppose I have an employees table in Postgres DB where I have to insert a value for an employee name which is john's

Since it's Postgres I will escape the single quote ' by doubling them up -> ''

So john's will become john''s

Now when I select that particular row/instance using select query I have to double the quote again. So to select the value john''s I have to write 'john''''s' and my query becomes -

select * from employees where name = 'john''''s'

Is this the best approach? or Is there any alternative to this process of data insertion and selection for these particular type of data (contains quote)? Any suggestion ?


Solution

  • No you don't have to double the escaped quotes:

    select * 
    from employees 
    where name = 'john''s'