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?
gamarrojgqConnect With a Mentor Commented:

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

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.

Replace a single with two, that's all you need
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.