Tuesday, November 10, 2015

SQL Server - Strip NameSpace from XML String

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION Strip_XML_NS
(
    @xml varchar(max)
)
RETURNS varchar(max)
AS
BEGIN
    DECLARE @stripped varchar(max)

    declare @i int
    set @i = CHARINDEX('"', @xml) - LEN('xmlns="')

    declare @f int
    set @f = CHARINDEX('"', @xml, CHARINDEX('"', @xml) + 1) + 1

    declare @ns varchar(max)
    set @ns = SUBSTRING(@xml, @i, @f - @i)

    set @stripped = REPLACE(@xml, @ns, '')
   
    RETURN @stripped
END
GO