40

so I am trying to run a script like this one:

select id
from owner 
where owner.name = "john's"

and I am getting this error: ERROR: column "john's" does not exist.

Also I tried like this: where owner.name = 'john\'s', but it dit not work

Anyone knows how I can run a query like this one?

Randy
  • 9,419
  • 5
  • 39
  • 56
Eugen
  • 463
  • 1
  • 4
  • 5
  • 5
    try double single quotes = "john ' ' s"? – JamieD77 Aug 01 '16 at 14:08
  • 1
    `e'john\'s'` should do the trick. (the e prefix allows c-style backslash quoting(and other things) in string literals) – joop Aug 01 '16 at 14:08
  • 2
    Single quotes go around string literals in Postgres. To escape a single quote inside your string literal, use two single quotes: `'John''s'` – JNevill Aug 01 '16 at 14:08
  • Double single quotes inside the string literal, i.e. `where owner.name = 'john''s'` – jarlh Aug 01 '16 at 14:09
  • 1
    If everything else fails, read the manual: https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS –  Aug 01 '16 at 15:08

2 Answers2

60

You can escape single quotes when you double them. For example:

= 'john''s'
manuzi1
  • 1,068
  • 8
  • 18
10

Try this

select id
from owner 
where owner.name = (E'john\'s')::text

Update: we can escape most of the characters using this statement

select id
from owner 
where owner.name = (E'john\character you want to escape's')::text
Paarth
  • 580
  • 3
  • 10