Results 1 to 7 of 7

Thread: Single Quotes in SQL statement

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Location
    Posts
    1
    I would like to be able to insert a statement into my table which contains a number of single quotes. For ex.,
    'John's friend'.
    What do I surround this statement with in my SQL to avoid errors?

    Thank you,

    Olga

  2. #2
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    Double quotes - "

  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Excuse me, Gentlemen ....

    JHausMann, I just want to clarify...

    Do you mean the string should be "John's Friend" ?

    Thanks in advance for your reply...

    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  4. #4
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    yup.

    "John's Friend"

  5. #5
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    I am not sure what version of VB you are using so here is a solution that will work for sure:

    Code:
    Public Function FixQuotes(A As String)
    
    Dim i As Integer
    i = InStr(A, "'")
    
    Do While i > 0
      A = Left(A, i) & Mid(A, i)
      i = InStr(i + 2, A, "'")
    Loop
    
    FixQuotes = A
    
    End Function
    Then use it in this fashion:

    Code:
    YourString = "John's Friend"
    YourString = FixQuotes(YourString)  ' String now is John''s Friend
    Also you could use this function to fix all text boxes on a form:

    Code:
    Public Sub FixFormQuotes(glCallfrm As Form)
    Dim mycontrol As Control
    For Each mycontrol In glCallfrm.Controls
    If TypeOf mycontrol Is TextBox Then
      mycontrol.Text = FixQuotes(mycontrol.Text)
    End If
    Next
    
    End Sub
    If you are using VB6 you could also use the replace function, just replace single quotes with two single quotes.

    Hope this helps,

  6. #6
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    It's kind of hard (not impossible, but hard) to wrap a SQL command in single quotes in VB, simply because the compiler will treat it as a comment....

  7. #7
    New Member
    Join Date
    Aug 2000
    Posts
    7
    There may be an even easier way to doing this. If you are using a sybase database try making a stored proceedure in the database and pass your string to it as an argument, it may even speed things up.
    "Oh damn why does this not work"
    "Every computer is a F.R.E.D, Freeking Ridiculous Electronic Device, the first word depending on your level of frustration"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width