简体   繁体   中英

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. 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. At the end, return the sum of all those values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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