Finding rows that don't contain numeric data in Oracle

前端 未结 9 578
陌清茗
陌清茗 2020-12-09 17:00

I am trying to locate some problematic records in a very large Oracle table. The column should contain all numeric data even though it is a varchar2 column. I need to find

9条回答
  •  星月不相逢
    2020-12-09 17:21

    In contrast to SGB's answer, I prefer doing the regexp defining the actual format of my data and negating that. This allows me to define values like $DDD,DDD,DDD.DD In the OPs simple scenario, it would look like

    SELECT * 
    FROM table_with_column_to_search 
    WHERE NOT REGEXP_LIKE(varchar_col_with_non_numerics, '^[0-9]+$');
    

    which finds all non-positive integers. If you wau accept negatiuve integers also, it's an easy change, just add an optional leading minus.

    SELECT * 
    FROM table_with_column_to_search 
    WHERE NOT REGEXP_LIKE(varchar_col_with_non_numerics, '^-?[0-9]+$');
    

    accepting floating points...

    SELECT * 
    FROM table_with_column_to_search 
    WHERE NOT REGEXP_LIKE(varchar_col_with_non_numerics, '^-?[0-9]+(\.[0-9]+)?$');
    

    Same goes further with any format. Basically, you will generally already have the formats to validate input data, so when you will desire to find data that does not match that format ... it's simpler to negate that format than come up with another one; which in case of SGB's approach would be a bit tricky to do if you want more than just positive integers.

提交回复
热议问题