Skip to content

Instantly share code, notes, and snippets.

@Cyberloki
Created September 22, 2013 22:51
Show Gist options
  • Save Cyberloki/6664613 to your computer and use it in GitHub Desktop.
Save Cyberloki/6664613 to your computer and use it in GitHub Desktop.
SQL AsXMLAttribute function
if object_id('AsXMLAttribute') IS NOT NULL
DROP FUNCTION [dbo].[AsXMLAttribute]
GO
CREATE FUNCTION [dbo].[AsXMLAttribute] (
@text varchar(max)
)
RETURNS varchar(max)
AS
BEGIN
declare @res varchar(max)
select @res = (select @text for xml path('') )
select @res = replace(@res, '"', '"')
select @res = replace(@res, '''', ''')
return @res
END
-- select dbo.AsXMLAttribute('This <here> is some ''crazy'' "stuff" & more')
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment