ORACLE - Select Count on a Subquery

后端 未结 3 1661
我寻月下人不归
我寻月下人不归 2021-01-22 04:06

I\'ve got an Oracle table that holds a set of ranges (RangeA and RangeB). These columns are varchar as they can hold both numeric and alphanumeric values, like the following exa

3条回答
  •  伪装坚强ぢ
    2021-01-22 04:21

    You can test each column with a regular expression to determine if it is a valid number:

    SELECT COUNT(1)
    FROM   table_of_ranges
    WHERE  CASE WHEN REGEXP_LIKE( RangeA, '^-?\d+(\.\d*)?$' )
                THEN TO_NUMBER( RangeA )
                ELSE NULL END
              < 10
    AND    REGEXP_LIKE( RangeB, '^-?\d+(\.\d*)?$' );
    

    Another alternative is to use a user-defined function:

    CREATE OR REPLACE FUNCTION test_Number (
      str VARCHAR2
    ) RETURN NUMBER DETERMINISTIC
    AS
      invalid_number EXCEPTION;
      PRAGMA EXCEPTION_INIT(invalid_number, -6502);
    BEGIN
      RETURN TO_NUMBER( str );
    EXCEPTION
      WHEN invalid_number THEN
        RETURN NULL;
    END test_Number;
    /
    

    Then you can do:

    SELECT COUNT(*)
    FROM   table_of_ranges
    WHERE  test_number( RangeA ) <= 10
    AND    test_number( RangeB ) IS NOT NULL;
    

提交回复
热议问题