SQL Server:从特定列中的字符串获取SO号

时间:2016-02-15 03:34:22

标签: sql sql-server

我有一个名为“PONDAILY”的表,其中有一个名为“PONCOM”的列。 “PONCOM”具有字符串内容,如文本注释,我需要从字符串中仅提取SO编号,下面是列中数据如何查看和突出显示的示例。我不能使用任何UDF,因为我只能读取SQL服务器。

DELETE TO EXPEDITE ON NEW STO FOR M/D SO SAP# **5000068**.  HN04

DELETING FOR M/D SO#**4500000762** KF 03-31

REDUCED BY 1 TO EXPEIDTE FOR M/D SO**100471**...CSR EM...HN04040

REDUCED QTY BY 1 TO EXPEDITE FOR M/D SO SAP **100551**. HN040300

REDUCED QTY BY 1 TO EXPEDITE FOR M/D SO SAP **100551**/1. HN040300

REDUCED BY QTY1 FOR M/D SO# **105707**

REDUCED QTY BY 3 FOR M/D SO#S **5003785**, 5003786, 5003791 KF 0

我想只获得粗体包含

1 个答案:

答案 0 :(得分:0)

如果我理解正确的问题:

select substring(PONCOM, t2.num, case when t3.not_num < t2.num then 8000 else t3.not_num - t2.num end)
  from PONDAILY p 
   cross apply (select charindex('SO', PONCOM, 1)) t1(so)
   cross apply (select patindex('%[0-9]%', substring(PONCOM, t1.so, 8000)) + t1.so - 1) t2(num)
   cross apply (select patindex('%[^0-9]%', substring(PONCOM, t2.num, 8000)) + t2.num - 1) t3(not_num)