Backslashes in SQL strings are illegal

written by jedi on April 10th, 2007 @ 02:41 PM

A popular way to escape quotes in SQL strings is to prepend a backslash:


SELECT * FROM a WHERE b = 'John\'s dog'
SELECT * FROM a WHERE b= "Gimme a \"quote\", man"

But this is actually:

  • Non-standard, non-portable
  • Only working with some database servers like MySQL and PostgreSQL
  • But it isn't even supported by default any more by recent PostgreSQL versions, and it might become deprecated by MySQL as well

The standard, portable, reliable way of inserting quotes in SQL strings is to write them twice:


SELECT * FROM a WHERE b = 'John''s dog'
SELECT * FROM a WHERE b = "Gimme a ""quote"", man"

And yes, it works with your current database server, even MySQL.

Comments

  • toma on 10 Apr 20:26

    Good to know, thanks

Comments are closed

Options:

Size

Colors