Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

single quote in data messing with single quote in SQL 1

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I have this as a customer name
Ms. C'Etter Sneed

and this code throws the error:
Syntax error (missing operator) in query expression ''Ms. C'Etter Sneed' as Name ....
Code:
SQLCode = SQLCode & Chr(39) & curcust.Name.GetValue & Chr(39) & " AS Name, "

How can I change it to allow a single quote in the data?


TIA

DougP
 
You need to REPLACE() the single quotes in your data with two consecutive single quotes. This an MS convention, and SQL Server will then play nicely with your data.


"Business conventions are important because they demonstrate how many people a company can operate without."
 
Oh Yeah Baby
Shes playing nice now!!
have a Star !!!
heres what I did
Code:
         If InStr(1, curcust.Name.GetValue, "'") Then
                                NewName = Replace(curcust.Name.GetValue, "'", "''")
                                SQLCode = SQLCode & Chr(39) & NewName & Chr(39) & " AS Name, "
                            Else
                                SQLCode = SQLCode & Chr(39) & curcust.Name.GetValue & Chr(39) & " AS Name, "
                            End If

DougP
 
This reminds me of a related problem I had once, when importing a text file into a payroll application. The root cause was obvious when I realized the import failed when it reached "John Smith, Jr." !
 
Just an FYI, the REPLACE function will basically perform the IF for you.... You could change all the above code to just:

Code:
SQLCode = SQLCode & Chr(39) & REPLACE(curcust.Name.GetValue, "'", "''") & Chr(39) & " AS Name, "

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top