如何在mysql中只获取String中的数字?

时间:2016-05-17 06:00:02

标签: mysql

我有一些包含字母数字值的字符串输出。我想从该字符串中只获取数字。我如何通过查询获取此信息?我可以使用哪些MySql功能?

我的查询如下:

select DISTINCT SUBSTRING(referrerURL,71,6)   
from   hotshotsdblog1.annonymoustracking   
where  advertiserid = 10 
limit  10;

输出:

100683
101313
19924&
9072&h
12368&
5888&h
10308&
100664
1&hash
101104

我希望输出如下:

100683
101313
19924
9072
12368
5888 
10308
100664
1
101104

12 个答案:

答案 0 :(得分:21)

如果字符串以数字开头,则包含非数字字符,您可以使用CAST()函数或通过添加0隐式将其转换为数字:

SELECT CAST('1234abc' AS UNSIGNED); -- 1234
SELECT '1234abc'+0; -- 1234

要从任意字符串中提取数字,您可以添加function之类的自定义this

DELIMITER $$

CREATE FUNCTION `ExtractNumber`(in_string VARCHAR(50)) 
RETURNS INT
NO SQL
BEGIN
    DECLARE ctrNumber VARCHAR(50);
    DECLARE finNumber VARCHAR(50) DEFAULT '';
    DECLARE sChar VARCHAR(1);
    DECLARE inti INTEGER DEFAULT 1;

    IF LENGTH(in_string) > 0 THEN
        WHILE(inti <= LENGTH(in_string)) DO
            SET sChar = SUBSTRING(in_string, inti, 1);
            SET ctrNumber = FIND_IN_SET(sChar, '0,1,2,3,4,5,6,7,8,9'); 
            IF ctrNumber > 0 THEN
                SET finNumber = CONCAT(finNumber, sChar);
            END IF;
            SET inti = inti + 1;
        END WHILE;
        RETURN CAST(finNumber AS UNSIGNED);
    ELSE
        RETURN 0;
    END IF;    
END$$

DELIMITER ;

定义函数后,您可以在查询中使用它:

SELECT ExtractNumber("abc1234def") AS number; -- 1234

答案 1 :(得分:13)

对于仍在寻找的人,请使用正则表达式:

select REGEXP_SUBSTR(name,"[0-9]+") as amount from `subscriptions`

答案 2 :(得分:2)

基于Eugene Yarmash答案。这是自定义函数的版本,该函数提取一个带两个小数位的小数。有利于价格提取。

DELIMITER $$

CREATE FUNCTION `ExtractDecimal`(in_string VARCHAR(255)) 
RETURNS decimal(15,2)
NO SQL
BEGIN
    DECLARE ctrNumber VARCHAR(255);
    DECLARE in_string_parsed VARCHAR(255);
    DECLARE digitsAndDotsNumber VARCHAR(255) DEFAULT '';
    DECLARE finalNumber VARCHAR(255) DEFAULT '';
    DECLARE sChar VARCHAR(1);
    DECLARE inti INTEGER DEFAULT 1;
    DECLARE digitSequenceStarted boolean DEFAULT false;
    DECLARE negativeNumber boolean DEFAULT false;

    -- FIX FIND_IN_SET cannot find a comma ","
    SET in_string_parsed = replace(in_string,',','.');

    IF LENGTH(in_string_parsed) > 0 THEN
        -- extract digits and dots
        WHILE(inti <= LENGTH(in_string_parsed)) DO
            SET sChar = SUBSTRING(in_string_parsed, inti, 1);
            SET ctrNumber = FIND_IN_SET(sChar, '0,1,2,3,4,5,6,7,8,9,.'); 
            IF ctrNumber > 0 AND (sChar != '.' OR LENGTH(digitsAndDotsNumber) > 0) THEN
                -- add first minus if needed
                IF digitSequenceStarted = false AND inti > 1 AND SUBSTRING(in_string_parsed, inti-1, 1) = '-' THEN
                    SET negativeNumber = true;
                END IF;

                SET digitSequenceStarted = true;
                SET digitsAndDotsNumber = CONCAT(digitsAndDotsNumber, sChar);
            ELSEIF digitSequenceStarted = true THEN
                SET inti = LENGTH(in_string_parsed);
            END IF;
            SET inti = inti + 1;
        END WHILE;

        -- remove dots from the end of number list
        SET inti = LENGTH(digitsAndDotsNumber);
        WHILE(inti > 0) DO
            IF(SUBSTRING(digitsAndDotsNumber, inti, 1) = '.') THEN
                SET digitsAndDotsNumber = SUBSTRING(digitsAndDotsNumber, 1, inti-1);
                SET inti = inti - 1;
            ELSE
                SET inti = 0;
            END IF;
        END WHILE;

        -- extract decimal
        SET inti = 1;
        WHILE(inti <= LENGTH(digitsAndDotsNumber)-3) DO
            SET sChar = SUBSTRING(digitsAndDotsNumber, inti, 1);
            SET ctrNumber = FIND_IN_SET(sChar, '0,1,2,3,4,5,6,7,8,9'); 
            IF ctrNumber > 0 THEN
                SET finalNumber = CONCAT(finalNumber, sChar);
            END IF;
            SET inti = inti + 1;
        END WHILE;

        SET finalNumber = CONCAT(finalNumber, RIGHT(digitsAndDotsNumber, 3));
        IF negativeNumber = true AND LENGTH(finalNumber) > 0 THEN
            SET finalNumber = CONCAT('-', finalNumber);
        END IF;

        IF LENGTH(finalNumber) = 0 THEN
            RETURN 0;
        END IF;

        RETURN CAST(finalNumber AS decimal(15,2));
    ELSE
        RETURN 0;
    END IF;    
END$$

DELIMITER ;

测试:

select ExtractDecimal("1234"); -- 1234.00
select ExtractDecimal("12.34"); -- 12.34
select ExtractDecimal("1.234"); -- 1234.00
select ExtractDecimal("1,234"); -- 1234.00
select ExtractDecimal("1,111,234"); -- 11111234.00
select ExtractDecimal("11,112,34"); -- 11112.34
select ExtractDecimal("11,112,34 and 123123"); -- 11112.34
select ExtractDecimal("-1"); -- -1.00
select ExtractDecimal("hello. price is 123"); -- 123.00
select ExtractDecimal("123,45,-"); -- 123.45

答案 3 :(得分:1)

在php中试试这个

$string = '9072&h';
echo preg_replace("/[^0-9]/", '', $string);// output: 9072

或点击此链接在MySql中执行此操作 Refer the link

答案 4 :(得分:1)

这是我对Eugene Yarmash对ExtractNumber函数的改进。

它不仅会去除非数字字符,还会去除&#[0-9];之类的HTML实体,它们也应被视为非数字unicode字符。

这是纯MySQL <8上没有UDP的代码。

CREATE DEFINER = 'user'@'host' FUNCTION `extract_number`(
        str CHAR(255)
    )
    RETURNS char(255) CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci
    DETERMINISTIC
    NO SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
  DECLARE tmp VARCHAR(255);
  DECLARE res VARCHAR(255) DEFAULT "";
  DECLARE chr VARCHAR(1);
  DECLARE len INTEGER UNSIGNED DEFAULT LENGTH(str);
  DECLARE i INTEGER DEFAULT 1;

  IF len > 0 THEN
    WHILE i <= len DO
      SET chr = SUBSTRING(str, i, 1);

      /* remove &#...; */
      IF "&" = chr AND "#" = SUBSTRING(str, i+1, 1) THEN
        WHILE (i <= len) AND (";" != SUBSTRING(str, i, 1)) DO
          SET i = i + 1;
        END WHILE;
      END IF;

      SET tmp = FIND_IN_SET(chr, "0,1,2,3,4,5,6,7,8,9");
      IF tmp > 0 THEN
        SET res = CONCAT(res, chr);
      END IF;
      SET i = i + 1;
    END WHILE;
    RETURN res;
  END IF;

  RETURN 0;
END;

但是,如果您使用的是UDP的PREG_REPLACE,则可以使用以下行:

RETURN PREG_REPLACE("/[^0-9]/", "", PREG_REPLACE("/&#[0-9]+;/", "", str));

答案 5 :(得分:1)

在这里我成功使用了这个功能:

select REGEXP_REPLACE('abc12.34.56-ghj^-_~#@!', '[^0-9]+', '')

输出:123456

解释:基本上我是在要求 mysql 将 0 到 9 之间的所有“非数字”替换为“”。

答案 6 :(得分:0)

尝试, 查询级别,

 SELECT CAST('1&hash' AS UNSIGNED);

for PHP,

echo intval('13213&hash');

答案 7 :(得分:0)

对于任何有类似要求的新人来说,这应该是你所需要的。

    select DISTINCT CONVERT(SUBSTRING(referrerURL,71,6), SIGNED) as `foo`
    from   hotshotsdblog1.annonymoustracking   
    where  advertiserid = 10 
    limit  10;

答案 8 :(得分:0)

我已将其重写为MemSQL语法:

DROP FUNCTION IF EXISTS GetNumeric;
DELIMITER //
CREATE FUNCTION GetNumeric(str CHAR(255)) RETURNS CHAR(255) AS
  DECLARE i SMALLINT = 1;
  DECLARE len SMALLINT = 1;
  DECLARE ret CHAR(255) = '';
  DECLARE c CHAR(1);

BEGIN
  IF str IS NULL
  THEN 
    RETURN "";
  END IF;

  WHILE i < CHAR_LENGTH( str ) + 1 LOOP
    BEGIN
      c = SUBSTRING( str, i, 1 );
      IF c BETWEEN '0' AND '9' THEN
        ret = CONCAT(ret,c);
      END IF;
      i = i + 1;
    END;
  END LOOP;
  RETURN ret;
 END //
DELIMITER ;

SELECT GetNumeric('abc123def456xyz789') as test;

答案 9 :(得分:0)

我建议使用数据透视表(例如,仅包含从1到至少字符串长度的有序数字向量的表),然后执行以下操作:

SELECT group_concat(c.elem separator '')
from (
select b.elem 
from
(
select substr('PAUL123f3211',iter.pos,1) as elem
from (select id as pos from t10) as iter 
where iter.pos <= LENGTH('PAUL123f3211')
) b
where b.elem REGEXP '^[0-9]+$') c

答案 10 :(得分:0)

可以用 PHP 代替。

foreach ($query_result as &$row) {
  $row['column_with_numbers'] = (int) filter_var($query_result['column_with_numbers'], FILTER_SANITIZE_NUMBER_INT);
}

答案 11 :(得分:0)

基于 Eugene Yarmash 和 Martins Balodis 的回答。

就我而言,我不知道源字符串是否包含点作为小数点分隔符。虽然,我知道应该如何处理特定的列。例如。如果 value 出现为“10,00”小时而不是“1.00”,我们知道最后一个分隔符应该被视为点小数点分隔符。为此,我们可以依靠辅助布尔参数来指定最后一个逗号分隔符的行为方式。

DELIMITER $$
CREATE FUNCTION EXTRACT_DECIMAL(
    inString            VARCHAR(255)
,   treatLastCommaAsDot BOOLEAN
) RETURNS varchar(255) CHARSET utf8mb4
    NO SQL
    DETERMINISTIC
BEGIN
    DECLARE ctrNumber             VARCHAR(255);
    DECLARE inStringParsed        VARCHAR(255);
    DECLARE digitsAndDotsNumber   VARCHAR(255) DEFAULT '';
    DECLARE digitsBeforeDotNumber VARCHAR(255) DEFAULT '';
    DECLARE digitsAfterDotNumber  VARCHAR(255) DEFAULT '';
    DECLARE finalNumber           VARCHAR(255) DEFAULT '';
    DECLARE separatorChar         VARCHAR(1)   DEFAULT '_';
    DECLARE iterChar              VARCHAR(1);
    DECLARE inti                  INT          DEFAULT 1;
    DECLARE digitSequenceStarted  BOOLEAN      DEFAULT false;
    DECLARE negativeNumber        BOOLEAN      DEFAULT false;

    -- FIX FIND_IN_SET cannot find a comma ","
    -- We need to separate entered dot from another delimiter characters.
    SET inStringParsed = TRIM(REPLACE(REPLACE(inString, ',', separatorChar), ' ', ''));

    IF LENGTH(inStringParsed) > 0 THEN
        -- Extract digits, dots and delimiter character.
        WHILE(inti <= LENGTH(inStringParsed)) DO
            -- Might contain MINUS as the first character.
            SET iterChar  = SUBSTRING(inStringParsed, inti, 1);
            SET ctrNumber = FIND_IN_SET(iterChar, CONCAT('0,1,2,3,4,5,6,7,8,9,.,', separatorChar));

            -- In case the first extracted character is not '.' and `digitsAndDotsNumber` is set.
            IF ctrNumber > 0 AND (iterChar != '.' OR LENGTH(digitsAndDotsNumber) > 0) THEN
                -- Add first minus if needed. Note: `inti` at this point will be higher than 1.
                IF digitSequenceStarted = FALSE AND inti > 1 AND SUBSTRING(inStringParsed, inti - 1, 1) = '-' THEN
                    SET negativeNumber = TRUE;
                END IF;

                SET digitSequenceStarted = TRUE;
                SET digitsAndDotsNumber  = CONCAT(digitsAndDotsNumber, iterChar);
            ELSEIF digitSequenceStarted = true THEN
                SET inti = LENGTH(inStringParsed);
            END IF;

            SET inti = inti + 1;
        END WHILE;
       
        -- Search the left part of string until the separator.
        -- https://stackoverflow.com/a/43699586
        IF (
            -- Calculates the amount of delimiter characters.
            CHAR_LENGTH(digitsAndDotsNumber) 
            - CHAR_LENGTH(REPLACE(digitsAndDotsNumber, separatorChar, SPACE(LENGTH(separatorChar)-1)))
        ) + (
            -- Calculates the amount of dot characters.
            CHAR_LENGTH(digitsAndDotsNumber)
            - CHAR_LENGTH(REPLACE(digitsAndDotsNumber, '.', SPACE(LENGTH(separatorChar)-1)))
        ) > 0 THEN
            -- If dot is present in the string. It doesn't matter for the other characters.
            IF LOCATE('.', digitsAndDotsNumber) != FALSE THEN
                -- Replace all special characters before the dot.
                SET inti = LOCATE('.', digitsAndDotsNumber) - 1;
                -- Return the first half of numbers before the last dot.
                SET digitsBeforeDotNumber = SUBSTRING(digitsAndDotsNumber, 1, inti);
                SET digitsBeforeDotNumber = REPLACE(digitsBeforeDotNumber, separatorChar, '');
            
                SET digitsAfterDotNumber  = SUBSTRING(digitsAndDotsNumber, inti + 2, LENGTH(digitsAndDotsNumber) - LENGTH(digitsBeforeDotNumber));
                SET digitsAndDotsNumber   = CONCAT(digitsBeforeDotNumber, '.', digitsAfterDotNumber);
            ELSE
                IF treatLastCommaAsDot = TRUE THEN
                    -- Find occurence of the last delimiter within the string.
                    SET inti = CHAR_LENGTH(digitsAndDotsNumber) - LOCATE(separatorChar, REVERSE(digitsAndDotsNumber));
                    -- Break the string into left part until the last occurrence of separator character.
                    SET digitsBeforeDotNumber = SUBSTRING(digitsAndDotsNumber, 1, inti);
                    SET digitsBeforeDotNumber = REPLACE(digitsBeforeDotNumber, separatorChar, '');
                
                    SET digitsAfterDotNumber  = SUBSTRING(digitsAndDotsNumber, inti + 2, LENGTH(digitsAndDotsNumber) - LENGTH(digitsBeforeDotNumber));
                    -- Remove any dot occurence from the right part.
                    SET digitsAndDotsNumber   = CONCAT(digitsBeforeDotNumber, '.', REPLACE(digitsAfterDotNumber, '.', ''));
                ELSE
                    SET digitsAndDotsNumber   =  REPLACE(digitsAndDotsNumber, separatorChar, '');
                END IF;
            END IF;
        END IF;
       
        SET finalNumber = digitsAndDotsNumber;
        IF negativeNumber = TRUE AND LENGTH(finalNumber) > 0 THEN
            SET finalNumber = CONCAT('-', finalNumber);
        END IF;

        IF LENGTH(finalNumber) = 0 THEN
            RETURN 0;
        END IF;

        RETURN CAST(finalNumber AS DECIMAL(25,5));
    ELSE
        RETURN 0;
    END IF;
END$$

DELIMITER ;
 

以下是一些用法示例:

-- 
-- SELECT EXTRACT_DECIMAL('-711,712,34 and 123123', FALSE);  -- -71171234.00000
-- SELECT EXTRACT_DECIMAL('1.234', FALSE);                   -- 1.23400
-- SELECT EXTRACT_DECIMAL('1,234.00', FALSE);                -- 1234.00000
-- SELECT EXTRACT_DECIMAL('14 9999,99', FALSE);              -- 14999999.00000
-- SELECT EXTRACT_DECIMAL('-149,999.99', FALSE);             -- -149999.99000
-- SELECT EXTRACT_DECIMAL('3 536 500.53', TRUE);             -- 3536500.53000
-- SELECT EXTRACT_DECIMAL('3,536,500,53', TRUE);             -- 3536500.53000
-- SELECT EXTRACT_DECIMAL("-1");                             -- -1.00000
-- SELECT EXTRACT_DECIMAL('2,233,536,50053', TRUE);          -- 2233536.50053
-- SELECT EXTRACT_DECIMAL('13.01666667', TRUE);              -- 13.01667
-- SELECT EXTRACT_DECIMAL('1,00000000', FALSE);              -- 100000000.00000
-- SELECT EXTRACT_DECIMAL('1000', FALSE);                    -- 1000.00000
-- ==================================================================================