My Technical Notes

Tuesday, 12 March 2013

XML Encoding a value in SQL Server

In SQL Server, there seems to be no built in function to XML-encode a string. However it can be easily done using FOR XML PATH(''):


DECLARE @str NVARCHAR(100) ; SET @str = 'Barnes & Nobles'
SELECT CONVERT(NVARCHAR(MAX), (SELECT @str FOR XML PATH('')))

Outputs:


Barnes & Nobles

No comments: