如何使用TSql找到最长的数字模式

时间:2011-10-22 05:03:48

标签: tsql

我在

下面写了一张表
Values
--------
12Null345XXX23456
6712356

Expected Output
----------------
23456
123

如何使用TSql(基于优先集)?

由于

1 个答案:

答案 0 :(得分:1)

那么,你想要连续数字的最长子串吗?也许这样的事情(这里重新创建原始表;如果它已经存在,跳过CTE并使用你的表)?:

;with ten as (
select i from (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) x(i)
), subs as (
select SUBSTRING('0123456789',s.i,l.i) as sub, l.i as run
from ten as s
join ten as l on s.i+l.i<=11
), t as (
select s
from (
values ('12Null345XXX23456'),('6712356'))x(s)
)
select x.sub  
from t
cross apply (select top 1 subs.sub 
             from subs 
             where CHARINDEX(subs.sub,t.s)>0 
             order by subs.run desc) x