Help w/SQL that contains variables that contain a single quote 
Author Message
 Help w/SQL that contains variables that contain a single 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://www.*-*-*.com/
----------------------------------------------------------------------------
-------------------------------------------
Programmers are to blame for world hunger, homelessness, and the depletion
of our rain forests...FYI



Mon, 29 Nov 1999 03:00:00 GMT  
 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/

Hi Brian  

 I am using VB 5.0 going to Oracle.  Your problem is with embeded quotes
in a string.  When the database server tries to resolve the SQL
statement it sees the embeded qoute as the end of the expression.  You
have to escape the embedded quotes.  

 I dont know the exact syntax in SQL Server, but this is how you do it
in Oracle.

insert into DummyTable
       (DummyColumn)
values ('O''Leary')

Good Luck

Stephen Polito



Tue, 30 Nov 1999 03:00:00 GMT  
 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, :-)



Fri, 03 Dec 1999 03:00:00 GMT  
 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 run into this problem often and using double quotes is the solution but
you can't get double quotes into your sql string because VB treats double
quotes as the end of the string.

you can fool VB by substituting Chr$(34) for the double quote. VB will
translate CHR$(34) into a double quote and you will be able to execute your
statement.

ex.:  SQL="Insert Into Main (lastname) Values(" & Chr$(34) & txtLastName &
Chr$(34) & ")"

Hope that helps
Paul Ross



Mon, 06 Dec 1999 03:00:00 GMT  
 Help w/SQL that contains variables that contain a single quote

Here is a function that I use to get around this problem.  It converts the
single quote (or apostrophe) to a double apostrophe, which will be
translated correctly.

'***************************************************************
'* The primary purpose of this function is to take care of names
'* that use the apostrophe, like O'Malley.  If the name does
'* have one, then make it double.
'***************************************************************
Function LookForApostrophes(ByVal TheName$) As String
  Dim i%
  ReDim parts_of_name(1) As String
  Dim num_parts%
  Dim the_name$

  num_parts% = 1
  the_name$ = TheName$
  i% = InStr(the_name$, "'")
  If i% <> 0 Then
    num_parts% = num_parts% + 1
    ReDim Preserve parts_of_name(num_parts%)
    parts_of_name(num_parts% - 1) = Left$(the_name$, i%) & "'"
    parts_of_name(num_parts%) = Mid$(the_name$, i% + 1)
    the_name$ = parts_of_name(num_parts%)

    i% = InStr(the_name$, "'")
    Do Until i% = 0
      num_parts% = num_parts% + 1
      ReDim Preserve parts_of_name(num_parts%)
      parts_of_name(num_parts% - 1) = Left$(the_name$, i%) & "'"
      parts_of_name(num_parts%) = Mid$(the_name$, i% + 1)
      the_name$ = parts_of_name(num_parts%)
      i% = InStr(the_name$, "'")
    Loop

    the_name$ = ""
    For i% = 1 To num_parts%
      the_name$ = the_name$ & parts_of_name(i%)
    Next i%
  End If 'i% <> 0

  LookForApostrophes = the_name$
End Function

If anyone has a better solution, I would be interested in knowing about it.
Thanx,
--
Lance King
BTNA USSEC Atlanta



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 run into this problem often and using double quotes is the solution but
> you can't get double quotes into your sql string because VB treats double
> quotes as the end of the string.

> you can fool VB by substituting Chr$(34) for the double quote. VB will
> translate CHR$(34) into a double quote and you will be able to execute
your
> statement.

> ex.:  SQL="Insert Into Main (lastname) Values(" & Chr$(34) & txtLastName
&
> Chr$(34) & ")"

> Hope that helps
> Paul Ross



Tue, 07 Dec 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. SQL and input data containing a single quote

2. working with strings containing single- and double-quotes

3. how to search on a field containing single/double quote

4. Insert string containing single quote

5. Finding records that contain single quotes

6. ??? INSERTing String containing Single-quote using ODBC

7. Find a textvalue containing a single quote

8. Inserting values containing single quotes...

9. Value single line text box with variable containing special characters

10. WHERE clause containing a variable in a SQL SELECT statement

11. VB3 Array containing variable length data (HELP!)

12. How to include double quote and single quote in SQL statement

 

 
Powered by phpBB® Forum Software