簡體   English   中英

T-SQL從字符串中提取十進制值

[英]T-SQL to pull decimal values from a string

好的,我要做的是從字符串中提取小數值。 我的問題是字符串不統一。 有些可能是6.9%或5.2mg / L,有些可能根本沒有數值。 我想要做的是返回字符串中的十進制(或整數)值,如果不存在則返回NULL。

我試過這個功能:

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

但這只返回沒有小數位的數字。

你只需要添加一個. 兩個PATINDEX表達式中的(點):

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

另一種方法是刪除字符串后面和字符串之前的字符。 以下表達式執行此操作:

select val, 
       stuff(stuff(val+'x', patindex('%[0-9][^0-9.]%', val+'x') + 1, len(val), ''
                  ), 1, patindex('%[0-9]%', val) - 1, '')
from (values ('test123 xxx'), ('123.4'), ('123.4yyyyy'), ('tasdf 8.9'), ('asdb'), ('.2345')) as t(val);

內部的stuff()刪除數字后面的字符。 +'x'處理數字位於字符串末尾時發生的問題。 第一部分處理數字前的部分。

這確實假設字符串中只有一個數字。 您可以使用where子句來檢查:

where val not like '%[0-9]%[^0-9.]%[0-9]%'

使用此功能,它也將返回Decimals Numbers

    CREATE Function udf_ExtractNumber (@String varchar(256))
RETURNS VARCHAR(256)
AS
BEGIN
DECLARE @AlphaNumeric varchar(256)
,@Res varchar(256)

SET @AlphaNumeric = @String
SET @Res = NULL

WHILE (PATINDEX('%[0-9]%', @AlphaNumeric) > 0 )
BEGIN
    IF (PATINDEX('%[0-9]%', @AlphaNumeric) >0 AND PATINDEX('%[0-9]%', @AlphaNumeric) < CHARINDEX('.', @AlphaNumeric))
                BEGIN 
                    SET @Res = CONCAT(@Res ,SUBSTRING(@AlphaNumeric, PATINDEX('%[0-9]%', @AlphaNumeric), 1) )
                    SET @AlphaNumeric = RIGHT(@AlphaNumeric,len(@AlphaNumeric)- PATINDEX('%[0-9]%', @AlphaNumeric))
                END
            ELSE IF (CHARINDEX('.', @AlphaNumeric) >0  AND CHARINDEX('.', @AlphaNumeric) < PATINDEX('%[0-9]%', @AlphaNumeric))
                BEGIN 
                    SET @Res = CONCAT(@Res ,SUBSTRING(@AlphaNumeric, CHARINDEX('.', @AlphaNumeric), 1) )
                    SET @AlphaNumeric = RIGHT(@AlphaNumeric,len(@AlphaNumeric)- CHARINDEX('.', @AlphaNumeric))
                END
            ELSE IF (PATINDEX('%[0-9]%', @AlphaNumeric) >0)
                BEGIN 
                    SET @Res = CONCAT(@Res, SUBSTRING(@AlphaNumeric, PATINDEX('%[0-9]%', @AlphaNumeric), 1) )
                    SET @AlphaNumeric = RIGHT(@AlphaNumeric,len(@AlphaNumeric)- PATINDEX('%[0-9]%', @AlphaNumeric))
                END
            ELSE IF (CHARINDEX('.', @AlphaNumeric) >0 )
                BEGIN 
                    SET @Res = CONCAT(@Res,SUBSTRING(@AlphaNumeric, CHARINDEX('.', @AlphaNumeric), 1))
                    SET @AlphaNumeric = RIGHT(@AlphaNumeric,len(@AlphaNumeric)- CHARINDEX('.', @AlphaNumeric))
    END

END
Return @Res
END

SELECT dbo.udf_ExtractNumber('AD645.23DGD') 提取十進制數

從字符串中提取Internet協議號

我認為我有最有效的方法,因為它不需要循環和最簡單,因為我在一個短行中完成所有字符串操作。 看看這個:

SELECT  string,
        CASE
            --If there's a number, then return it
            WHEN PATINDEX('%[0-9]%',string) != 0 
                --Pretty much find the first number and last number, then return that section(It's off by one so I so I add 1 at the end)
                THEN SUBSTRING(string,PATINDEX('%[0-9]%',string),DATALENGTH(string) - PATINDEX('%[0-9]%',REVERSE(string)) + 1)
            --If there are no numbers, return NULL
            ELSE NULL
        END return_int
FROM
(
    SELECT '123456789.3243421341% of mg/L blah blah blah whitespace    ' AS string
    UNION ALL
    SELECT 'No numbers here'
) A

結果:

string                                                      return_int
----------------------------------------------------------- -----------------------------------------------------------
123456789.3243421341% of mg/L blah blah blah whitespace     123456789.3243421341
No numbers here                                             NULL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM