apostrophes in SQL

I am writing a sql statement and using ADO to execute it through a connection....the problem is that if a user enters an apostrophe (like in O'reilly) or something, it screws up the SQL statement.  How do I avoid this?
LVL 2
jedimikeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SynstheCommented:
You have to escape the apostrophe, like so -->  \'

You can either enter this directly into statement itself, or if your statement is built based on outside input (via a form, etc) you should use a regular expression statement to search for any apostrophe's (such as that one) and replace them with the escaped version (\').
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jedimikeAuthor Commented:
so in other words...
this:
insert into table(name) VALUES('Mike O'Reilly);

becomes this:
insert into table(name) VALUES('Mike O\'Reilly);
0
jedimikeAuthor Commented:
forgot the ' at the end:...
insert into table(name) VALUES('Mike O\'Reilly');
0
SynstheCommented:
That's exactly what I meant, yup. Give it a try. :)
0
adinasCommented:
Just Use the Replace function.

MyText = 'Mike O'Reilly'
Replace = Replace(MyText ,"'", "\'")

PS the HTML value for ' is &#39
So you might want to use:
Replace(MyText ,"'", "&#39")
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.