I need to wrap a few strings in single quotes for a dynamic TSQL statement in a stored procedure. I am absolutely certain that no single quote values will be passed(these fields are not "editable" at the application level, only selectable) hence my requirements are pretty mild in this aspect.
The solution I came up with is simple but nice:
declare @SingleQuote nvarchar(1)
select @SingleQuote = ''''
Then use it all over the place :-)
It would be helpful, however, if there was a better way, i.e. an SQL function just like the newid() one to generate a new GUID.
I would then just need to do something like:
select.....SingleQuotes(MyField)....
Any suggestion?
Thank you for your time reading this,
Andrew
The simplest method to escape single quotes in SQL is to use two single quotes. For example, if you wanted to show the value O'Reilly, you would use two quotes in the middle instead of one. The single quote is the escape character in Oracle, SQL Server, MySQL, and PostgreSQL.
Single quotes are used to indicate the beginning and end of a string in SQL. Double quotes generally aren't used in SQL, but that can vary from database to database. Stick to using single quotes. That's the primary use anyway.
select QUOTENAME(FieldName, CHAR(39))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With