Passing strings to queries with quotes

F

F R

Hello,

I have a databse with lastnames containing the " ' " (quote) character e.g.
O'Niell

When I attempt to pass this string to a query; the query fails as the string
that is being passed has a quote in it which makes the string illegal.

How can I pass a field that contains the quotes witout making the code fail.

Thanks,
Frank
 
W

Wayne Morgan

There are a couple of options.

1) Use double quotes to delimit the string instead of single quotes. This
way, the single quote won't be the delimiter and so won't cause the problem.
Depending on how you're doing this, you may need to double-up the double
quotes to get them inserted.

2) Replace the single quote in the string with 2 single quotes.

Both of these options work on the fact that if you use a delimiter in the
string, to get it to be taken as a literal character, you have to double it
(i.e. "Yes, I really want the character in the string").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top