Andrea Raimondi
Andrea Raimondi

Reputation: 519

SQLServer function for single quotes

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

Upvotes: 5

Views: 5766

Answers (1)

cmsjr
cmsjr

Reputation: 59175

select QUOTENAME(FieldName, CHAR(39))

Upvotes: 13

Related Questions