ORACLE - Select Count on a Subquery

后端 未结 3 1663
我寻月下人不归
我寻月下人不归 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:40

    Try this query:

      SELECT COUNT(*)
      FROM table R
      WHERE translate(R.RangeA, 'x0123456789', 'x') = 'x' and
            translate(R.RangeB, 'x0123456789', 'x') = 'x'
    

    First, you don't need the subquery for this purpose. Second, using to_number() or upper()/lower() are prone to other problems. The function translate() replaces each character in the second argument with values from the third argument. In this case, it removes numbers. If nothing is left over, then the original value was an integer.

    You can do more sophisticated checks for negative values and floating point numbers, but the example in the question seemed to be about positive integer values.

提交回复
热议问题