在ntext类型字段中会有些HTML标记,比如<div>/<br>/<font>等,当我们做统计查询时,这些内容会影响显示效果,下面编写的一个SQL函数可以去除这么标记。
USE [COMMON]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[fun_RemoveHtml]
(
@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,' ','')
END