在sql

时间:2019-05-10 15:00:04

标签: sql string tsql charindex

我无法从字符串中提取字符选择

以下是来自“ TXT”字段的值的摘要:

  • FNP,2018年10月9日,PO-00123456-F333330- FA_002056
  • FNP,18-09-2018,PO-00987654-F010122- FA_002056
  • FNP,28/12/2017,PO-00123987-F10101
  • FNP,13-03-2019,FRPO-35412-F27272-ANNUL PO

我希望提取F333330,F010122,F10101和F27272

我已经尝试过charindex,left / right和substring,但是还无法破解

SELECT TXT , 
CASE WHEN CHARINDEX('-',(SUBSTRING(txt,CHARINDEX('-',txt,2)+1,99)))=0
THEN 
LTRIM(RTRIM(SUBSTRING(txt,CHARINDEX('-',txt,2)+1,99)))
ELSE 
LTRIM(RTRIM(SUBSTRING(txt,CHARINDEX('-',txt,2) +1,CHARINDEX('-',SUBSTRING(txt,CHARINDEX('-',txt,2)+1,99))-1)))
END
FROM #test

我想要的行集只是显示:

  • F333330
  • F010122
  • F10101
  • F27272

我尝试了不带case语句的子字符串的其他变体,但是没有运气。有人可以帮忙吗?

谢谢, 数词

3 个答案:

答案 0 :(得分:1)

这里是使用parsename()CROSS APPLY

的一个选项

示例

Declare @YourTable table (Txt varchar(500))
Insert Into @YourTable values
('FNP, 10/09/2018,PO-00123456 - F333330- FA_002056')
,('FNP, 18-09-2018,PO-00987654 - F010122- FA_002056')
,('FNP, 28/12/2017,PO-00123987 - F10101')
,('FNP, 13-03-2019,FRPO-35412 - F27272-ANNUL PO')

Select A.Txt
      ,NewValue = case when parsename(S,3) is null then parsename(S,1) else parsename(S,2) end
 From  @YourTable A
 Cross Apply (values ( replace(replace(parsename(replace(TXT,',','.'),1),'PO-','PO'),'-','.') )) B(S)

返回

Txt                                                  NewValue
FNP, 10/09/2018,PO-00123456 - F333330- FA_002056     F333330
FNP, 18-09-2018,PO-00987654 - F010122- FA_002056     F010122
FNP, 28/12/2017,PO-00123987 - F10101                 F10101
FNP, 13-03-2019,FRPO-35412 - F27272-ANNUL PO         F27272

答案 1 :(得分:0)

尝试一下?

DECLARE @test TABLE (TXT VARCHAR(500));
INSERT INTO @test SELECT 'FNP, 10/09/2018,PO-00123456 - F333330- FA_002056' UNION ALL
SELECT 'FNP, 18-09-2018,PO-00987654 - F010122- FA_002056' UNION ALL
SELECT 'FNP, 28/12/2017,PO-00123987 - F10101' UNION ALL 
SELECT 'FNP, 13-03-2019,FRPO-35412 - F27272-ANNUL PO';
SELECT SUBSTRING(TXT, PATINDEX('%- F%', TXT) + 2, CASE WHEN CHARINDEX('-', SUBSTRING(TXT, PATINDEX('%- F%', TXT) + 2, 99)) = 0 THEN 99 ELSE CHARINDEX('-', SUBSTRING(TXT, PATINDEX('%- F%', TXT) + 2, 99)) - 1 END)
FROM @test;

...或者,多排一行:

DECLARE @test TABLE (TXT VARCHAR(500));
INSERT INTO @test SELECT 'FNP, 10/09/2018,PO-00123456 - F333330- FA_002056' UNION ALL
SELECT 'FNP, 18-09-2018,PO-00987654 - F010122- FA_002056' UNION ALL
SELECT 'FNP, 28/12/2017,PO-00123987 - F10101' UNION ALL 
SELECT 'FNP, 13-03-2019,FRPO-35412 - F27272-ANNUL PO' UNION ALL
SELECT 'FNP, 11/10/2017,PO-00112311 - F32121 regul po 112311';
SELECT SUBSTRING(TXT, PATINDEX('%- F%', TXT) + 2, 
    CASE 
        WHEN CHARINDEX('-', SUBSTRING(TXT, PATINDEX('%- F%', TXT) + 2, 99)) != 0 THEN CHARINDEX('-', SUBSTRING(TXT, PATINDEX('%- F%', TXT) + 2, 99)) - 1 
        WHEN CHARINDEX(' ', SUBSTRING(TXT, PATINDEX('%- F%', TXT) + 2, 99)) != 0 THEN CHARINDEX(' ', SUBSTRING(TXT, PATINDEX('%- F%', TXT) + 2, 99)) - 1 
        ELSE 99
    END)
FROM @test;

...或重复次数较少的奖金(使用CTE):

WITH x AS (
    SELECT SUBSTRING(TXT, PATINDEX('%- F%', TXT) + 2, 99) AS TXT FROM @test)
SELECT SUBSTRING(TXT, 0, 
    CASE 
        WHEN CHARINDEX('-', TXT) != 0 THEN CHARINDEX('-', TXT) - 1
        WHEN CHARINDEX(' ', TXT) != 0 THEN CHARINDEX(' ', TXT) - 1
        ELSE 99
    END)
FROM x;

答案 2 :(得分:0)

我知道我来晚了一点,但这是一种快速有效的方法,它假定文本以F开头并且您知道好的值的最小/最大长度。对于此示例,我假设该值的长度为5-7个字符,但是可以轻松调整。

DECLARE @YourTable table (SomeId INT IDENTITY PRIMARY KEY, Txt varchar(500))
Insert Into @YourTable values
('FNP, 10/09/2018,PO-00123456 - F333330- FA_002056')
,('FNP, 18-09-2018,PO-00987654 - F010122- FA_002056')
,('FNP, 28/12/2017,PO-00123987 - F10101')
,('FNP, 13-03-2019,FRPO-35412 - F27272-ANNUL PO')


SELECT 
  t.SomeID, 
  ItemLength = MAX(r.N), 
  ItemIndex  = MAX(p.P),
  Item       = SUBSTRING(MAX(t.Txt),MAX(p.P),MAX(r.N)+1)
FROM @YourTable AS t
CROSS JOIN  (VALUES(4),(5),(6))                          AS r(N)
CROSS APPLY (VALUES('%F'+REPLICATE('[0-9A-Z]',r.N)+'%')) AS f(P)
CROSS APPLY (VALUES(PATINDEX(f.P,t.Txt)))                AS p(P)
WHERE p.P > 0
GROUP BY t.SomeId

返回:

SomeID      ItemLength  ItemIndex   Item
----------- ----------- ----------- ---------
1           6           31          F333330
2           6           31          F010122
3           5           31          F10101
4           5           30          F27272