
Help w/SQL that contains variables that contain a single quote
Quote:
> I have a very large SQL statement (Insert - in this case) that negotiates
> variables into strings for use within the SQL. My problem is, I use the
> single quote to force the negotiation of the variables and sometimes the
> variable contents include a single quote (like in a last name -
> O'Leary...). How can I get around this???
> I am using VB5.0 Enterprise on a Pentuim 200, accessing MS SQL 6.50.201
> TIA...
> ----------------
> Brian G. Burke - Senior Programmer Analyst
> COBOL - PowerBuilder - VB 5.0
> http://coyote.accessnv.com/wholsale/
> ----------------------------------------------------------------------------
> -------------------------------------------
> Programmers are to blame for world hunger, homelessness, and the depletion
> of our rain forests...FYI
There are a few solutions to this :-
1) Use double-quotes
Example: Insert into table1 values ("O'Leary", ...
^ ^
2) Use two single-quotes
Example: Insert into table1 values ('O''Leary', ...
^^
2) Write a function to replace the occurrence of single quotes into
two single-quotes
Example: var1 = Replace("O'Leary", "'", "''")
var1 returns --> O''Leary
I may give you the codes for this function, quite an easy one, :-)
Public Function Replace(strIn As String, strOccur As String, strReplWith
As String) As String
Dim nPos As Integer
Dim strTemp As String
Dim strTemp2 As String
strTemp = strIn
nPos = 1
nPos = InStr(nPos, strTemp, strOccur)
Do While nPos > 0
strTemp = Mid(strTemp, 1, nPos - 1) & strReplWith _
& Mid(strTemp, nPos + 1)
nPos = InStr(nPos + 2, strTemp, strOccur)
Loop
Replace = strTemp
End Function
Hope it solves your problem, :-)