Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

apostrophes and double quotes

Status
Not open for further replies.

kjoost001

Programmer
Apr 11, 2002
19
0
0
US
does anyone know how to eliminate the problem of double quotes and apostrophes.
if there is an apostrophe in my sql statement, it causes a break in the string. thus, when I try to do an insert from my .asp page, it fails. It is really annoying and I was wondering if someone could help me. Is there a way to parse the string and if an apostrophe or double quote appears replace it with "'"?? I dont know.

Thanks,
Ken
 
Just use

Replace(string, "'", "''")

The easiest way to do this is to change the value that you are putting into your SQL string. Something like this:

sql = sql & "column = '" & Replace(value, "'", "''") & "'"

 
You could use Replace to replace any single apostrophe with a string of 2 singles, e.g. MyField = Replace(MyField,"'","''").

Steve
 
SQL uses '' as one ', so it works fine if you replace those with two single quots before you write it back to the database as others told you before me.

In addition be careful when you display it on screen or sending it out as a variable value too when reading it out from your database.
Use the Server.HTMLEncode(ObjectName("VariableName")) method to display the content from a recordset, and always put your content between " double quots (using the ascii code), instead of ' single quots:

Like writing out an alt tag like that:

Response.Write &quot;<img src='image.jpg' alt=&quot; & chr(34) & Server.HTMLEncode(ObjectName(&quot;VariableName&quot;)) & chr(34) & &quot;>&quot;

because if the single quots are not in pair that can make your HTML look ungly too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top