function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sforceincanadasforceincanada 

Apostrophes

Hi,

I want to do a select that uses apostrophes for example:

"select id from lead where Email = ' "+thisemail+" ' "

And am getting malformed query errors how can I avoid this?

Thanks,

Graham

DevAngelDevAngel

Hi sforceincanada,

You will need to escape the apostrophe.

where name = 'O/'Conner'

The / is the escapte char.

Erik ForsbergErik Forsberg

Huh ?

What do one do if there is a '/' in the string to begin with ?

Do we need to escape an ordinary slash too ?

 

VinnieBVinnieB
Actually, it's the other slash.  Write a simple function that reads each character in a string and escapes it if it's an apostophre. String SQL_escape(String string) { String r_string = ""; if (string == null) { return null; } for (int i = 0; i < string.length() ; i++ ) { if (string.charAt(i) == '\'') { r_string += "\\'"; } else { r_string += string.charAt(i); } } return r_string; } Then, escape the variables before using them in a query, i.e.; qString += SQL_escape(sess_lastName);