简体   繁体   English

SQL-仅从列中获取包含负值的数字,而不包含字母和字符

[英]SQL - get only numbers including minus value not alphabets and character from a column

I have a column which has numbers(including negative values),alphabets,alphanumeric and single character symbol(like -,&,@). 我有一列包含数字(包括负值),字母,字母数字和单个字符符号(如-,&,@)。

How to get rid of alphabets,alphanumeric and symbols and get the sum of the column. 如何摆脱字母,字母数字和符号并获得列的总和。

I have to use the condition only in select statement not in where condition. 我只需要在select语句中使用条件,而不能在where条件中使用。 Because it should not affect other column results. 因为它不应该影响其他列的结果。

This is what I've tried: 这是我尝试过的:

SELECT COUNT(*), CASE
    WHEN REGEXP_LIKE(REMD_PART_CSN, '^-?[0-9]\d*(\.\d+)?$') THEN SUM(REMD_PART_CSN)
    ELSE NULL
END
FROM <TABLE>

列值

How about this? 这个怎么样?

SELECT
    COUNT(*),
    SUM(
        CASE
            WHEN REGEXP_LIKE(REMD_PART_CSN, '^-?\d+(\.\d+)?$')
                THEN CAST(REMD_PART_CSN AS NUMBER)
            ELSE 0
        END
    )
FROM yourtable

Ie for every row, if REMD_PART_CSN looks like a number, then convert it to a number, else use 0 instead. 即对于每一行,如果REMD_PART_CSN看起来像一个数字,则将其转换为数字,否则使用0代替。 At the end, return the sum of all those values. 最后,返回所有这些值的总和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM