Search code examples
excelt-sqlsql-server-2008-r2excel-2013vba

Passing Variable With Apostrophe To SQL Query


I am using Excel to run paramaterized SQL Server Queries. Everything works fine, except when the parameter has a apostrophe. For example, let's say my syntax is this

Select * from testtable where arovaica = @passedvalue

And in Excel @passedvalue is set to Michael's I get an error of

Incorrect syntax

How should I encapsulate this variable with an apostrophe in order for SQL to compile properly?


Solution

  • Try a replace as suggested here

        Select * from testtable where arovaica = REPLACE(@passedvalue, '', '''')