You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过SQL提取HTML字符串中指定格式的字段名与NotificationId

解析SQL中HTML字段内的RESULTFLD字段名需求及实现方法

问题背景

表结构:

  • 表名:Template
  • 字段:
    • NotificationId(int类型)
    • Template(varchar(max)类型,存储HTML内容)

示例数据:

NotificationId = 1,Template字段内容:

<body>
<table border="1">
<tr><td>{RESULTFLD:0:customer_name}</td></tr>
<tr><td>{RESULTFLD:1:customer_address1}</td></tr>
<tr><td>{RESULTFLD:2:customer_address2}</td></tr>
</body>
</table>

需求:解析HTML内容,获取对应NotificationId,以及所有{RESULTFLD:N:字段名}格式中的字段名,输出格式如下:

NotificationId 1
customer_name
customer_address1
customer_address2

注:RESULTFLD后的数字递增不影响解析,核心是匹配格式提取字段名。


实现方案

方案一:SQL Server 端直接解析

适用于SQL Server 2016及以上版本,利用字符串拆分和字符定位实现:

WITH CTE_Extract AS (
    SELECT 
        NotificationId,
        TRIM(value) AS FldSegment
    FROM Template
    CROSS APPLY STRING_SPLIT(Template, '{')
    WHERE value LIKE 'RESULTFLD:%}'
)
SELECT 
    CASE WHEN ROW_NUMBER() OVER(PARTITION BY NotificationId ORDER BY (SELECT NULL)) = 1 
         THEN 'NotificationId ' + CAST(NotificationId AS VARCHAR(10)) 
         ELSE SUBSTRING(FldSegment, CHARINDEX(':', FldSegment, CHARINDEX(':', FldSegment) + 1) + 1, LEN(FldSegment) - CHARINDEX(':', FldSegment, CHARINDEX(':', FldSegment) + 1) - 1)
    END AS OutputLine
FROM CTE_Extract
ORDER BY NotificationId, 
         CAST(SUBSTRING(FldSegment, CHARINDEX(':', FldSegment) + 1, CHARINDEX(':', FldSegment, CHARINDEX(':', FldSegment)+1) - CHARINDEX(':', FldSegment) -1) AS INT)

逻辑说明:

  1. 按{}拆分HTML内容,筛选出包含RESULTFLD的片段;
  2. 对每个片段,提取第二个:之后、}之前的内容(即字段名);
  3. 控制首行输出NotificationId,后续行输出字段名;
  4. 按RESULTFLD后的数字排序,保证输出顺序和原HTML一致。

方案二:程序端(以C#为例)解析

如果SQL端处理复杂,可在程序中读取数据后用正则匹配:

using System;
using System.Text.RegularExpressions;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString = "你的数据库连接字符串";
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();
            string sql = "SELECT NotificationId, Template FROM Template WHERE NotificationId = @Id";
            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                cmd.Parameters.AddWithValue("@Id", 1); // 可替换为目标NotificationId
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        int notificationId = reader.GetInt32(0);
                        string templateHtml = reader.GetString(1);
                        
                        // 正则匹配{RESULTFLD:N:字段名}格式,捕获字段名
                        Regex regex = new Regex(@"{RESULTFLD:\d+:(\w+)}");
                        MatchCollection matches = regex.Matches(templateHtml);
                        
                        // 输出结果
                        Console.WriteLine($"NotificationId {notificationId}");
                        foreach (Match match in matches)
                        {
                            Console.WriteLine(match.Groups[1].Value);
                        }
                    }
                }
            }
        }
    }
}

逻辑说明:

  1. 用正则表达式精准匹配目标格式,通过捕获组直接提取字段名;
  2. 匹配结果按原HTML中的顺序返回,直接遍历输出即可;
  3. 可快速扩展处理多个NotificationId的批量解析场景。

内容的提问来源于stack exchange,提问作者user2208746

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 03:15:59