代码改变世界

去除HTML标签--SQL写法

2009-07-27 15:25  hailibu  阅读(862)  评论(2编辑  收藏  举报
-- =============================================
--
 Author:        Derry
--
 Create date: 2009-07-27
--
 Description:    去除HTML标签
--
 =============================================
ALTER FUNCTION [dbo].[StripAllTags]
(
    
@input    VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
    
declare 
    
@Result varchar(8000),
    
@start int,
    
@end int,
    
@len int

    
set @input = @input+'<>'
    
set @Result = ''
    
set @len=len(@input)
    
set @start = charindex('<',@input,1)
    
set @end = charindex('>',@input,@start)
    
while(@start<@end)
        
begin        
            
if(@start<>1
              
set @Result = @Result + substring(@input,1,@start-1)
            
set @len = @len - @end
            
set @input = substring(@input,@end+1,@len)
            
set @start = charindex('<',@input,1)
            
set @end = charindex('>',@input,@start)
        
end

    
RETURN replace(@Result,'&nbsp;','')    
END