Select 字符串中字符前的第一个整数

Select first int before characters in string

添加到 this 论坛响应背后的逻辑,我如何 select 第一个 int 与字符串中的一组特定尾随字符?换句话说,我想从 "this thing is 15 in long!"

中提取“15 in”

我尝试了以下方法(基于答案)...

SELECT LEFT(subsrt, PATINDEX('%[^0-9]%', subsrt + 't') - 1)
FROM (
     SELECT subsrt = SUBSTRING(string, pos, LEN(string))
     FROM (
          SELECT string, pos = PATINDEX('%[0-9] in%', string)
          FROM @temp
     ) d
) t

...但它只会从示例字符串中提取“5 in”,而不是“15 in”。因此,我不仅需要整个 int 值(此脚本的最初目的是找到字符串中的第一个完整 int 值),而且我需要字符串中 "in" 之前的整个 int 值。

我知道这可能是重复的 post,一旦我获得足够的声誉,我会将其作为评论添加到原始论坛的答案中,然后在需要时将其删除 post。

如果您的示例是“5 in”,则添加一个额外的“[0-9]”

DECLARE @temp TABLE
(
      string NVARCHAR(50)
)

INSERT INTO @temp (string)
VALUES 
    ('bob this thing is 15 in long!')

SELECT SUBSTRING(string, pos, 5)
FROM (
    SELECT string, pos = PATINDEX('%[0-9][0-9] in%', string)
    FROM @temp
) d