T-SQL SQL Server 2014中的HTML转义

时间:2018-10-09 14:31:21

标签: sql-server tsql sql-server-2014

我在SQL Server数据库中有一列,该列以以下方式存储文本块:

<HTML><HEAD><style type="text/css">BODY,TD,TH,BUTTON,INPUT,SELECT,TEXTAREA{FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial,Helvetica;}BODY{MARGIN: 5px;}P,DIV,UL,OL,BLOCKQUOTE{MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px;}</style></HEAD><BODY> <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">Patient is a&nbsp;84 year old female.&nbsp; Patient's histpry includes the following:</p> <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p></BODY></HTML>​

我想从上面的这个特定示例中带回来的所有内容是:

Patient is an 84 year old female. Patient's histpry includes the following:

老实说,我什至不知道从哪里开始,SQL Server 2014中是否有任何HTML转义类型函数?我无权访问CLI,我将需要在已创建任务的存储过程中运行代码。

2 个答案:

答案 0 :(得分:2)

如果对表值函数开放,请考虑以下内容。

我厌倦了提取字符串(左,右,charindex,patindex,reverse等),我修改了一个split / parse函数以接受两个非相似的分隔符。在这种情况下,></

另外,作为TVF,如果数据在表中,则很容易合并到CROSS APPLY中。

示例

Declare @S varchar(max)='<HTML><HEAD><style type="text/css">BODY,TD,TH,BUTTON,INPUT,SELECT,TEXTAREA{FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial,Helvetica;}BODY{MARGIN: 5px;}P,DIV,UL,OL,BLOCKQUOTE{MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px;}</style></HEAD><BODY> <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">Patient is a&nbsp;84 year old female.&nbsp; Patient''s histpry includes the following:</p> <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p></BODY></HTML>​'

Select *
 From  [dbo].[tvf-Str-Extract](replace(@S,'&nbsp;',' '),'>','</')
 Where RetVal<>' '
   and RetVal not like 'BODY,%'

返回

RetSeq  RetPos  RetVal
2       284     Patient is a 84 year old female.  Patient's histpry includes the following:

注意: WHERE是可选的,可能需要进行调整以适合您的实际需求。只是为了好玩,请在没有WHERE的情况下尝试。另外,在此示例中,我们困住了&nbsp;,但正如您所知道的,可能还有很多其他问题,即&mdash;

感兴趣的功能

CREATE FUNCTION [dbo].[tvf-Str-Extract] (@String varchar(max),@Delimiter1 varchar(100),@Delimiter2 varchar(100))
Returns Table 
As
Return (  

with   cte1(N)   As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
       cte2(N)   As (Select Top (IsNull(DataLength(@String),0)) Row_Number() over (Order By (Select NULL)) From (Select N=1 From cte1 N1,cte1 N2,cte1 N3,cte1 N4,cte1 N5,cte1 N6) A ),
       cte3(N)   As (Select 1 Union All Select t.N+DataLength(@Delimiter1) From cte2 t Where Substring(@String,t.N,DataLength(@Delimiter1)) = @Delimiter1),
       cte4(N,L) As (Select S.N,IsNull(NullIf(CharIndex(@Delimiter1,@String,s.N),0)-S.N,8000) From cte3 S)

Select RetSeq = Row_Number() over (Order By N)
      ,RetPos = N
      ,RetVal = left(RetVal,charindex(@Delimiter2,RetVal)-1) 
 From  (
        Select *,RetVal = Substring(@String, N, L) 
         From  cte4
       ) A
 Where charindex(@Delimiter2,RetVal)>1

)
/*
Max Length of String 1MM characters

Declare @String varchar(max) = 'Dear [[FirstName]] [[LastName]], ...'
Select * From [dbo].[tvf-Str-Extract] (@String,'[[',']]')
*/

答案 1 :(得分:2)

对于HTML,您永远无法确定将XML强制转换成功。但是,将&nbsp;替换为简单的空格后,您可能会这样:

Declare @S varchar(max)='<HTML><HEAD><style type="text/css">BODY,TD,TH,BUTTON,INPUT,SELECT,TEXTAREA{FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial,Helvetica;}BODY{MARGIN: 5px;}P,DIV,UL,OL,BLOCKQUOTE{MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px;}</style></HEAD><BODY> <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">Patient is a&nbsp;84 year old female.&nbsp; Patient''s histpry includes the following:</p> <p style="MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p></BODY></HTML>​'

SELECT CAST(REPLACE(@S,'&nbsp;',' ') AS XML).value('(//p/text())[1]','nvarchar(max)');

结果

Patient is a 84 year old female.  Patient's histpry includes the following: