0

我有一张这样的桌子

Declare @Temp Table(Data VarChar(20))

Insert Into @Temp Values('F_200_100_')
Insert Into @Temp Values('F_50_')
Insert Into @Temp Values('F_30_')
Insert Into @Temp Values('F_50_10')
Insert Into @Temp Values('F_100_')
Insert Into @Temp Values('F_20_')

我希望我的输出是从数据列中提取的数字的不同值

20
30
50
100
200

我尝试过使用 patindex,但我正在寻找尝试过的想法

select
Left(
SubString(Data, PatIndex('%[0-9]%', Data), 8000), 
PatIndex('%[^0-9]%', SubString(Data, PatIndex('%[0-9]%', Data), 8000) + 'X')-1
)
from @temp

参考 http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/extracting-numbers-with-sql-server

4

1 回答 1

0

试试这个:

DECLARE @YourTable table (RowID int, Layout varchar(200))
INSERT INTO @YourTable 
select ROW_NUMBER() over (order by (select 0)) as rn,replace(RIGHT(data,len(data)-CHARINDEX('_',data,1)),'_',',') from temptab

;WITH SplitSting AS
(
    SELECT
        RowID,LEFT(Layout,CHARINDEX(',',Layout)-1) AS Part
            ,RIGHT(Layout,LEN(Layout)-CHARINDEX(',',Layout)) AS Remainder
        FROM @YourTable
        WHERE Layout IS NOT NULL AND CHARINDEX(',',Layout)>0
    UNION ALL
    SELECT
        RowID,LEFT(Remainder,CHARINDEX(',',Remainder)-1)
            ,RIGHT(Remainder,LEN(Remainder)-CHARINDEX(',',Remainder))
        FROM SplitSting
        WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)>0
    UNION ALL
    SELECT
        RowID,Remainder,null
        FROM SplitSting
        WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)=0
)
SELECT distinct cast(part as int) FROM SplitSting where len(part) > 0  order by cast(part as int)
于 2012-11-28T06:46:16.977 回答