简体   繁体   English

SQL-解析列中的HTML数据

[英]SQL - Parse HTML Data in Column

I have a column named "Message". 我有一列名为“消息”。 In this column there is a data which is HTML code. 在此列中,有一个HTML代码数据。 I need to parse this HTML in SQL then split it 5 different column "Name" - "Surname" - "Email" - "Telephone" - "Message". 我需要在SQL中解析此HTML,然后将其拆分为5个不同的列“名称”-“姓氏”-“电子邮件”-“电话”-“消息”。 Here is the HTML format that I need to parse; 这是我需要解析的HTML格式;

<html>
   <body>
      <br><br>
      <table>
         <tr>
            <td>NameSurname</td>
            <td>kaydi peldi sord</td>
         </tr>
         <tr>
            <td>Email</td>
            <td>...@gmail.com</td>
         </tr>
         <tr>
            <td>Telephone</td>
            <td>535...5464</td>
         </tr>
         <tr>
            <td colspan=2>Message</td>
         </tr>
         <tr>
            <td colspan=2>Benfica-Fenerbahçe</td>
         </tr>
      </table>
   </body>
</html>

First, split NameSurname to Name and Surname. 首先,将NameSurname拆分为Name和Surname。 The rule is split from last space (in this sample, it should "Name : ejder mehmet" , "Surname : sıkık", then insert other columns directly. How can I do that? Thanks for answers! 该规则与最后一个空格分开(在此示例中,该规则应为“名称:ejder mehmet”,“姓氏:sıkık”,然后直接插入其他列。我该怎么做?谢谢您的回答!

I'm a year late, it's not pretty, and it's definitely not 100% safe, but this does the job for me on the rare occasions I need to parse HTML. 我迟到了一年,它并不漂亮,而且绝对不是100%安全的,但这在我需要解析HTML的极少数情况下对我来说是有用的。 Create this function first. 首先创建此功能。

CREATE FUNCTION dbo.StringBetweenTwoPatterns (@PrePattern varchar(max) @PostPattern varchar(max), @string varchar(max)) 
RETURNS varchar(Max)
AS 
BEGIN   
DECLARE @WildPre VARCHAR(MAX) = '%' + @PrePattern + '%'
DECLARE @WildPost VARCHAR(MAX) = '%' + @PostPattern + '%'
IF PATINDEX(@WildPre, @String) > 0
    AND PATINDEX(@WildPost, @String) > 0
BEGIN
    DECLARE @RIGHT VARCHAR(MAX) = SUBSTRING(@string, PATINDEX(@WildPre,@string) + LEN(@PrePattern), LEN(@string))
    RETURN LEFT(@RIGHT,(PATINDEX(@WildPost,@RIGHT) - 1))
END
RETURN NULL
END
GO

When you call this function, you have to keep full formatting and white space in the search strings, so it's going to look like this: 调用此函数时,您必须在搜索字符串中保留完整格式和空格,因此它将如下所示:

SELECT [NameSurname] = StringBetweenTwoPatterns('<td>NameSurname</td>
        <td>','</td>',[Message]

Splitting Name and Surname is something you should be able to extrapolate from the the substring, right, left, and patindex examples above. 拆分名称和姓氏应该可以从上面的子字符串,右,左和patindex示例中推断出来。 Or just google some other answers for that. 或只是谷歌一些其他答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM