I’ve a table with a varchar column (A) and another integer column(B) indicating the type of data present in A. If B is 0, then A will always contain numeric digits.
So when I form an sql like this
SELECT COUNT(*) FROM TAB WHERE B = 0 AND TO_NUMBER(A) = 123;
I get an exception invalid number.
I expect B = 0 to be evaluated first, and then TO_NUMBER(A) second, but from the above scenario I suspect TO_NUMBER(A) is evaluated first. Is my guess correct?
In contrast to programming languages like C, C#, Java etc., SQL doesn’t have so called conditional logical operators. For conditional logical operators, the right operand is only evaluated if it can influence the result. So the evaluation of
&&stops if the left operand returns false. For||it stops if the left operand returns true.In SQL, both operands are always evaluated. And it’s up to the query optimizer to choose which one is evaluated first.
I propose you create the following function, which is useful in many cases:
Then you can rewrite your query as:
You can also use the function to check whether a string is a number.