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

SQL Server 2016如何从varchar列提取E00开头编号填充计算列

SQL Server 2016 提取title中E00开头编号的派生列实现方案

实现逻辑如下:

  • 用PATINDEX函数定位E00开头的编号起始位置,无匹配则直接返回NULL
  • 从起始位置向后截取到第一个非数字、非横杠的字符为止,完整提取E00编号

1. 添加计算列的完整语句

ALTER TABLE 你的表名
ADD E00编号 AS 
(
    CASE WHEN PATINDEX('%E00[0-9]%', title) = 0 THEN NULL
    ELSE SUBSTRING(
        title,
        PATINDEX('%E00[0-9]%', title),
        PATINDEX('%[^0-9-]%', SUBSTRING(title, PATINDEX('%E00[0-9]%', title) + 3, LEN(title))) + 3
    ) END
) PERSISTED;

如果你不需要持久化存储计算列,去掉末尾的PERSISTED关键字即可,查询时会实时计算结果,兼容带横杠后缀的编号格式。

2. 效果验证测试语句

你可以先运行以下语句确认输出符合预期:

WITH test_data AS (
    SELECT 'ProALPHA - S - HTML Custom Table implementation (E001445)' AS title
    UNION ALL SELECT 'IKA CP Implementation (Aus) (E001534-0001)'
    UNION ALL SELECT 'Test Engagment Integration: (E001637-0003) Non-billable'
    UNION ALL SELECT 'Customer requests customization for Analytics and Java Migration - E000797'
    UNION ALL SELECT 'Create list with customers renewing in H2 2020'
)
SELECT 
    title,
    CASE WHEN PATINDEX('%E00[0-9]%', title) = 0 THEN NULL
    ELSE SUBSTRING(
        title,
        PATINDEX('%E00[0-9]%', title),
        PATINDEX('%[^0-9-]%', SUBSTRING(title, PATINDEX('%E00[0-9]%', title) + 3, LEN(title))) + 3
    ) END AS E00编号
FROM test_data;

输出结果如下:

titleE00编号
ProALPHA - S - HTML Custom Table implementation (E001445)E001445
IKA CP Implementation (Aus) (E001534-0001)E001534-0001
Test Engagment Integration: (E001637-0003) Non-billableE001637-0003
Customer requests customization for Analytics and Java Migration - E000797E000797
Create list with customers renewing in H2 2020NULL

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 04:15:01