Finding rows that don't contain numeric data in Oracle

前端 未结 9 579
陌清茗
陌清茗 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:06

    After doing some testing, building upon the suggestions in the previous answers, there seem to be two usable solutions.

    Method 1 is fastest, but less powerful in terms of matching more complex patterns.
    Method 2 is more flexible, but slower.

    Method 1 - fastest
    I've tested this method on a table with 1 million rows.
    It seems to be 3.8 times faster than the regex solutions.
    The 0-replacement solves the issue that 0 is mapped to a space, and does not seem to slow down the query.

    SELECT *
    FROM 
    WHERE TRANSLATE(replace(,'0',''),'0123456789',' ') IS NOT NULL;
    
    
    

    Method 2 - slower, but more flexible
    I've compared the speed of putting the negation inside or outside the regex statement. Both are equally slower than the translate-solution. As a result, @ciuly's approach seems most sensible when using regex.

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

    提交回复
    热议问题