在sql上只选择数值

时间:2014-12-11 05:55:14

标签: sql regex postgresql conditional

我知道已经存在一些相同的问题,但这个问题让我感到困惑。

我有这个问题:

SELECT 
    CASE
    WHEN COALESCE(substring(location_name FROM '[0-9]+'), location_name) != '' 
    THEN COALESCE(substring(location_name FROM '[0-9]+'), location_name)
    ELSE '1'
END AS sequence
FROM LOCATION

我想从该查询得到的是:

  1. 如果location_name不包含任何数字值,请返回1
  2. 如果location_name包含数值,则只获取最后一个数值(在字符串后面), 即c2轮播10,应仅返回10
  3. 如果location_name仅包含数字值,则返回1
  4. 但我得到的是:

    location_name   expected result     what I get
                                        using [0-9]         using [0-9]+
    carousel 1      1                   1                   1
    carousel 2      2                   2                   2
    carousel 3      3                   3                   3
    carousel 12     12                  1                   12
    bottom banner   1                   bottom banner       bottom banner
    c2 carousel 1   1                   2                   2
    c2 carousel 3   3                   2                   2
    59977           1                   5                   59977
    

    是否可以在sql中执行此操作?

2 个答案:

答案 0 :(得分:1)

试试这个:(我认为它会返回预期的结果。否则请记下你得到的结果。)

SELECT location_name, 
CASE WHEN concat('',location_name * 1) = location_name THEN
     1
WHEN concat('',reverse(substring_index(reverse(location_name), ' ', 1)) * 1) = reverse(substring_index(reverse(location_name), ' ', 1)) THEN
    reverse(substring_index(reverse(location_name), ' ', 1))
ELSE
    1
END AS EXPECTED_RESULT
FROM YourTable

--Quick Demo here:MySQL

当您突然将标签MYSql更改为PgSQL时,这并不好。我的所有作品都浪费了。

这是PgSQL代码:

创建一个检查isdigit()的函数:

create function isdigits(text) returns boolean as '
select $1 ~ ''^(-)?[0-9]+$'' as result
' language sql;

然后是代码:

   SELECT location_name, 
      CASE WHEN isdigits(location_name) = true THEN 1
           WHEN isdigits(substr(location_name, length(regexp_replace(location_name, '\\s\\S+$', '')) + 2)) = true THEN 
                substr(location_name, length(regexp_replace(location_name, '\\s\\S+$', '')) + 2)::int
           ELSE 1
      END AS Result
    FROM YourTable

--Quick Demo - PGSQL.

答案 1 :(得分:0)

-- Create one function first which will help to check wether value is numeric or not

CREATE FUNCTION mysql_IsNumeric(val TEXT) RETURNS int(11)
RETURN val REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$';

--create function that return numeric values only    
CREATE FUNCTION mysql_NumericOnly(val TEXT) RETURNS text CHARSET latin1
BEGIN  
DECLARE idx INT DEFAULT 0;  
IF mysql_IsNumeric(val) = 0 THEN  
IF ISNULL(val) THEN RETURN NULL; END IF;  
IF LENGTH(val) = 0 THEN RETURN ""; END IF;  
SET idx = LENGTH(val);  
WHILE idx > 0 DO   
IF strcmp(SUBSTRING(val,idx,1),'.')!=0  and mysql_IsNumeric(SUBSTRING(val,idx,1)) = 0 THEN  
SET val = REPLACE(val,SUBSTRING(val,idx,1),"");  
SET idx = LENGTH(val)+1;  
END IF;  
SET idx = idx - 1;  
END WHILE;  
END IF;  
RETURN val;  
END;


select *,
 case when mysql_IsNumeric(location_name)=1 then 1
 case when location_name!=mysql_NumericOnly(location_name) THEN
mysql_NumericOnly(Reverse(Left(REVERSE(location_name),INSTR(REVERSE(location_name),' ')))
  else 1 end as reselut    
  from YourTable