T-SQL- 如何使用 where 'like' 查询提取字符串中的模式 '(yyyy)'

T-SQL- How to extract pattern '(yyyy)' in string with where 'like' query

我有一个像 'wordword (2018)' 这样的字符串数据,想提取那些带有模式 (yyyy) 的数据。已尝试使用 '%/([0-9][0-9][0-9][0-9]/)%' 但不起作用

基于 HABO 的评论,您可以使用类似的东西:

DECLARE @Pattern VARCHAR(50) = '%([0-9][0-9][0-9][0-9])%'
SELECT A.value, yyyy = SUBSTRING(A.value, NULLIF(PATINDEX(@pattern, A.Value), 0) + 1, 4)
FROM (
    VALUES
        ('wordword (2018)'),
        ('Nothing here'),
        ('this (2010) and that (2020)')
) A(value)

SQL 服务器对模式匹配的支持非常有限,因此我将您的正则表达式转换为最接近 SQL 服务器支持的内容。上面的 NULLIF() 将零的 not-found 索引转换为 null,它传播到结果。

你试过CHARINDEX吗?

SUBSTRING(@str, CHARINDEX(‘[0-9][0-9][0-9][0-9]’,@str),4)