2016-12-09 86 views
0

我不確定sql是否可行。我正在嘗試從字符串中提取所有數值。從sql中提取字符串中的所有值

declare @mytable table 
(
myvalue varchar (50) 
) 

insert @mytable 
select 'DOBIH3HA3' UNION ALL 
select 'TAARE567ZAMEEN5' UNION ALL 
select 'GAG645JAMU43' 

下面的方法是非常封閉的,但沒有得到所需的輸出。

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

請先分享您的經驗....感謝很多

回答

3
CREATE FUNCTION dbo.fn_ExtractNumeric(@input VARCHAR(8000)) 
RETURNS VARCHAR(8000) 
AS 
BEGIN 
    DECLARE @n INT 
    SET @n = PATINDEX('%[^0-9]%', @input) 
    BEGIN 
     WHILE @n > 0 
     BEGIN 
      SET @input = STUFF(@input, @n, 1, '') 
      SET @n = PATINDEX('%[^0-9]%', @input) 
     END 
    END 
    RETURN ISNULL(@input,0) 
END 
GO 

declare @mytable table 
(
myvalue varchar (50) 
) 

insert @mytable 
select 'A2SK3HSDSK3' UNION ALL 
select 'KGI6620GYUIG' UNION ALL 
select 'GAG4444BY9Y' 

SELECT dbo.fn_ExtractNumeric(myvalue) 
FROM @mytable 
1

,我們需要創建function

CREATE FUNCTION dbo.udf_GetNumeric (@strAlphaNumeric VARCHAR(256)) 
RETURNS VARCHAR(256) 
AS 
BEGIN 
    DECLARE @intAlpha INT 
    SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric) 
    BEGIN 
     WHILE @intAlpha > 0 
     BEGIN 
      SET @strAlphaNumeric = STUFF(@strAlphaNumeric, @intAlpha, 1, '') 
      SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric) 
     END 
    END 
    RETURN ISNULL(@strAlphaNumeric,0) 
END 
GO 




create table #mytable 
(
myvalue varchar (50) 
) 

insert #mytable 
select 'A2SK3HSDSK3' UNION ALL 
select 'KGI6620GYUIG' UNION ALL 
select 'GAG4444BY9Y' 

SELECT dbo.udf_GetNumeric(myvalue) 
from #mytable 

    output 

    233 
    6620 
    44 
    449 
1
 -- ================================================ 
    -- Template generated from Template Explorer using: 
    -- Create Scalar Function (New Menu).SQL 
    -- 
    -- Use the Specify Values for Template Parameters 
    -- command (Ctrl-Shift-M) to fill in the parameter 
    -- values below. 
    -- 
    -- This block of comments will not be included in 
    -- the definition of the function. 
    -- ================================================ 
    SET ANSI_NULLS ON 
    GO 
    SET QUOTED_IDENTIFIER ON 
    GO 
    -- ============================================= 
    -- Author:  <Author,,Name> 
    -- Create date: <Create Date, ,> 
    -- Description: <Description, ,> 
    -- ============================================= 
    ALTER FUNCTION numonly 
    (
     -- Add the parameters for the function here 
     @p nvarchar(max) 
    ) 
    RETURNS nvarchar(max) 
    AS 
    BEGIN 
     -- Declare the return variable here 
     DECLARE @ResultVar nvarchar(max) 

     DECLARE @L int = LEN(@P) 
     WHILE @L > 0 
     BEGIN 
      SELECT @ResultVar = SUBSTRING(@P , @L , 1) + COALESCE(@ResultVar, '') WHERE SUBSTRING(@P , @L , 1) BETWEEN '0' AND '9'; 
      SET @L = @L - 1; 
     END 

     -- Return the result of the function 
     RETURN @ResultVar 

    END 
    GO 



select dbo.numonly(myvalue) from @mytable; 
2

通過使用臨時表相符並且交叉應用

Select A.* 
     ,B.* 
From @mytable A 
Cross Apply (
     Select String=(Select Substring(A.myvalue,N,1) 
         From (Select Top (Len(A.myvalue)) N=Row_Number() Over (Order By Number) From master..spt_values) NA 
         Where Substring(A.myvalue,N,1) Like '[0-9]' 
         For XML Path('')) 
) B 

返回

myvalue   String 
A2SK3HSDSK3  233 
KGI6620GYUIG 6620 
GAG4444BY9Y  44449