繁体   English   中英

SQL 如何从标准列捕获/检查特定文本以填充计算列的值

[英]SQL How to capture/check a particular text from standard column to populate value for calculated column

在 SQL Server 2016 中,我有一个带有 varchar 列(标题)的表,其中一些值如下

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

我想根据上面的标题列填充计算(派生)列的值,这样,

查找如果标题列包含文本 E00,则只有派生列将具有 E00 值,否则 NULL

例子,

预期产出

谢谢

假设我的问题是该值始终为字符串后缀,或者您想要的值始终以右括号为后缀,那么您可以使用一些CHARINDEXSUBSTRING执行以下操作:

USE Sandbox;
GO

SELECT V.Title,
       SUBSTRING(V.Title,E.CI,RP.CI - E.CI) AS YourColumn
FROM (VALUES('ProALPHA - S - HTML Custom Table implementation (E001445)'),
            ('IKA CP Implementation (Aus) (E001534-0001)'),
            ('Test Engagment Integration: (E001637-0003) Non-billable'),
            ('Customer requests customization for Analytics and Java Migration - E000797'),
            ('Create list with customers renewing in H2 2020'))V(Title)
      CROSS APPLY (VALUES(NULLIF(CHARINDEX('E00',V.Title),0)))E(CI)
      CROSS APPLY (VALUES(ISNULL(NULLIF(CHARINDEX(')',V.Title,E.CI),0),LEN(V.Title)+1)))RP(CI);

db<>小提琴

一种选择是使用CharindexSubstring的组合。 请注意,下面的100应该只是列的声明长度 - charindex 将始终停在字符串的末尾。

这不需要结束括号,它会查找最后一个数字。

with t as (
    select * from (values 
        ('ProALPHA - S - HTML Custom Table implementation (E001445)'                                    ),
        ('IKA CP Implementation (Aus) (E001534-0001)'                                                                   ),
        ('Test Engagment Integration: (E001637-0003) Non-billable'                                      ),
        ('Customer requests customization for Analytics and Java Migration - E000797'   ),
        ('Create list with customers renewing in H2 2020'                                                           )
    )t(title)
)
select title, 
   Iif(title like '%E00%',Reverse(Substring(part,patindex('%[0-9]%',part),100)),null)
from t
cross apply (values( Reverse(Substring(t.title, CharIndex('E00',t.title), 100 )) ))x(part)

示例小提琴

暂无
暂无

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

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