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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help handling quotation marks in SQL insert

Status
Not open for further replies.

SmileyFace

Programmer
Sep 10, 2002
99
US
Hey Guys. Really need your help with this one.

I am inserting data into an SQL server table from my asp page but need a function to handle the single quotation marks. I basically need to read the string being inserted into the table field and replace any single quotation marks (') with double quotes (") before inserting else I will get an error. PLEASE HELP me with this!! Would really appreciate if someone could help me with the script.

Thanks a lot!
 
please don't double post in multi forums
thread333-649136

[smile]

_____________________________________________________________________
Where have all my friends gone to????
onpnt2.gif

 
To replace all the single quotes in string TST by double quotes, just do this:
Code:
TST=Replace(TST,"'",Chr(34))

Hope This Help
PH.
 
Hey onpnt! Thanks for all the information. I wasn't aware of the SQL Injection Attack. Will have to take that into consideration now. One more thing...I post in multiple forums to invite more responses. I didn't intend to make you respond several times. Sorry.

Hey PH! I did use the 'Replace' statement but instead of Chr(34) just used """ and it worked fine. Thanks anyways. Could you tell me why you used Chr(34)?
 
To make the code more readable for me, as I'm often lost with too many """ :)

Hope This Help
PH.
 
I think you should use :
Code:
TST=Replace(TST,"'","''")

which is the correct way to escape single quotes (') in SQL Server - better than changing them to double quotes. This way you can do stuff like search text columns for a single quote (comes up often in surnames, eg O'Connell).

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top