How to handle single quotes in data in SQL INSERT statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Smecker
    New Member
    • Oct 2011
    • 11

    #1

    How to handle single quotes in data in SQL INSERT statement

    I'm working on a database for a list of users and have an import function that reads lines from an Excel file then builds a SQL statement to insert the data into the table. In one of my test runs one of the users had a single quote in their name and messed up the SQL. Here's a snippet of my SQL statement. The variable are all Strings, so as far as I know they need the single quotes around them. Thus if a user has a single quote in their name it throws off the SQL. Is there any way to cleanse the data or get SQL to ignore the quote in the name?

    Code:
    strSQL = "INSERT INTO [AmadeusUsers] ([Status],[FirstName],[MiddleName],[LastName]) "
    strSQL = strSQL + "VALUES ('" & Status & "','" & FirstName & "','" & MiddleName & "','" & LastName & "')"
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Use the replace function to double up on the quotes in the data.
    'O''Connor'

    Comment

    • Smecker
      New Member
      • Oct 2011
      • 11

      #3
      Worked perfectly, thanks!

      Comment

      Working...