Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 Strip HTML

Author  Topic 

nathans
Aged Yak Warrior

938 Posts

Posted - 2009-06-17 : 17:37:54
We've done this a few times here, usually with a loop. I thought I'd give a go using a numbers table.

if object_id('dbo.ufn_StripHTML') is not null
begin
drop function dbo.ufn_StripHTML
end
go

create function dbo.ufn_StripHTML
( @Input varchar(8000),
@Delimiter char(1)
)
returns varchar(8000)
as
begin

declare @Output varchar(8000)
select @Input = replace(replace(@input, '<', @Delimiter), '>', @Delimiter)

select @Output = isnull(@Output, '') + s
from ( select row_number() over (order by n.n asc) [i],
substring(@Delimiter + @Input + @Delimiter, n.n + 1, charindex(@Delimiter, @Delimiter + @Input + @Delimiter, n.n + 1) - n.n - 1) [s]
from dbo.Number n
where n.n = charindex(@Delimiter, @Delimiter + @Input + @Delimiter, n.n) and
n.n <= len(@Delimiter + @Input)
) d
where i % 2 = 1

return @Output

end
go

select dbo.ufn_StripHTML('<a>1<b>2</b>3<c>4<d ddd="ddd"><>5<5>', '|')
go


if object_id('dbo.ufn_StripHTML') is not null
begin
drop function dbo.ufn_StripHTML
end
   

- Advertisement -