Inserting ascii character into a sql server database

I am using Sql Server 2012.

In VB.NET, I have a situation where some of the data I am reading from a text file has double single apostrophes in the last name for a persons name.
The code eventually inserts the name into the database using a stored procedure.

 My approach to fix the double single apostrophe issue is to replace the double single quote with the Ascii value equivalent of Char(39).
This work perfectly. Then a stored procedure is called to insert NameStr into a database table.

 Dim NameStr As String = "O''Donnel"
 Dim Apostrophe As Char = Char(39)
 NameStr = NameStr.Replace("''",NameStr)

 So based on my above approach, does anyone know if this would work for earlier versions of sql server, such as sql server 2000 or sql server 2005?
 I don't have any version earlier than 2012, so I am not able to test for different versions.
LVL 2
brgdotnetcontractorAsked:
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.

Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

Yes this should work on earlier versions as well..

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
Bill PrewIT / Software Engineering ConsultantCommented:
I think what you really wanted/meant would be the following.  And yes, this should work fine in earlier versions of SQL, depending on what code you are sending to the database.  For example if you were doing some sort of dynamic SQL and concatenating strings and SQL clauses to build a query, then a string variable with a single quote in it could be a problem, and might actually have to be replaced by double quotes there.  But if everything is working fine on your version of SQL Server then I doubt your code is doing that.  Just something to be aware of...

 Dim NameStr As String = "O''Donnel"
 Dim Apostrophe As Char = Char(39)
 NameStr = NameStr.Replace("''",Apostrophe)

Open in new window

Which could also be simplified to just:
 Dim NameStr As String = "O''Donnel"
 NameStr = NameStr.Replace("''","'")

Open in new window


»bp
brgdotnetcontractorAuthor Commented:
Thanks everyone. Rihkin gave me an answer a few weeks ago, but I was not able to get online and mark the solution as complete. Both suggestions are good. Thanks.
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
Visual Basic.NET

From novice to tech pro — start learning today.