HTML Escape in T-SQL SQL Server 2014

匆匆过客 提交于 2019-12-12 12:51:44

问题


I have a column in a SQL Server database that stores a text block in the following fashion:

<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>​

All I want to bring back from this particular example above would be:

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

I honestly do not even know where to start, is there any HTML escape type functions in SQL Server 2014? I do not have access to CLI and I will need to run the code inside of a stored procedure that I have been tasked with creating.


回答1:


If open to a Table-Valued Function, consider the following.

Tired of extracting strings (left, right, charindex, patindex, reverse, etc), I modified a split/parse function to accept two non-like delimiters. In this case > and </

Also, being a TVF, it is easy to incorporate into a CROSS APPLY if you data is in a table.

Example

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,%'

Returns

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

Note: The WHERE is optional and may have to be tweaked to suite you actual needs. Just for fun, try it without the WHERE. Also, in this example, we trapped the &nbsp;, but as you know, there may be many others i.e. &mdash;.

The Function if Interested

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,'[[',']]')
*/



回答2:


With HTML you never can be sure, that the cast to XML will be successful. But, after replacing &nbsp; with simple blanks, you might go like this:

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)');

The result

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


来源:https://stackoverflow.com/questions/52723494/html-escape-in-t-sql-sql-server-2014

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!