Search code examples
sqlms-access

SQL to Query text in access with an apostrophe in it


I am trying to query a name (Daniel O'Neal) in column names tblStudents in an Access database, however Access reports a syntax error with the statement:

Select * from tblStudents where name like 'Daniel O'Neal'

due to the apostrophe in the name.

How do I overcome this?


Solution

  • You escape ' by doubling it, so:

    Select * from tblStudents where name like 'Daniel O''Neal' 
    

    Note that if you're accepting "Daniel O'Neal" from user input, the broken quotation is a serious security issue. You should always sanitize the string or use parametrized queries.