How to read data with single quote embedded in the data

I need to read data from another table.  It contains text that has embedded single quotes - i.e. O'Rouke.  I think I need to use the Replace function to replace the single quote with 3 single quotes. Is this right?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Can you specify what environment/language you're trying to accomplish this in?

For example in T-SQL a single quote can be escaped by using another single quote, so yes if you are in SQL then you can use 3 single quotes:

print 'test'  = test
print '''test'''  = 'test'

If that is the case, then your statement can look something like this

declare @my_string varchar(10)

set @my_string = 'test''s'

print @my_string  -- test's

set @my_string = replace(@my_string, '''', '''''')

print @my_string  -- test''s

What do you want to do with the data? Insert it in anohter table? assign  to variables? you want to use the Replace Function in a Query or in Code?

Anyway if is in Code you should use 2 singles quotes.
jsaacsonAuthor Commented:
I am using vb.2008 to read data from a sql table and re-write it to another sql table.  
I build the sql command in my program and then execute a non-query containing the command I created.  I get an error when I try to write the command because it contains a field I read that has an embedded single quote (apostrophe) in the field.

I was pretty sure the replace function would do the job, but not sure as to the syntax and how many single quotes to use in the replace function to make it work.


If it is from code you need to use 2 single quotes, we assume that your sql command is in a variable named strSQL, so your code should be like this:

strSQL.Replace("'", "''")

Open in new window


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
Replace a single with two, that's all you need
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
.NET Programming

From novice to tech pro — start learning today.