Question

I am trying to do a comparison based on a column. Let's say, if column>5.

select * where column>5

The column contains non digits. I thought Oracle allows one to compare strings (like Java).

Apparently this is not allowed.

ORA-01722: invalid number
01722. 00000 -  "invalid number"

Is there a way to do comparisons with non numeric fields?
Thanks

Was it helpful?

Solution

Yes, you have to put the 5 in quotes :

select * from table where column > '5'

OTHER TIPS

To shed a bit more light on why it doesn't work.

When using a number literal 5 instead of a character literal '5' Oracle does an implicit data type conversion and tries to convert all values in your table to a number. That's why you get that error.

You should never rely on implicit data type conversion. That is bound to give you trouble sometime.

Now if you correctly compare a character literal ('5') against a character column, no data type conversion is needed and no error occurs.

However: if you expect Oracle to actually do a numeric comparison then you are mistaken. Character string are based on ASCII values. Therefor the (character) value '10' is lower than the (character) value '2' because the first character '1' is ranked lower than '2'.

If the column is varchar2, then this:

select * from some_table where some_column > 5

... does an implicit conversion of all the column values to numbers, so you're really doing:

select * from some_table where to_number(some_column) > 5

It's the to_number() that's causing the ORA-01722, even though you can't see it, when it hits a value that is not numeric. The function being called on the column value also stops any index being used (oversimplifying a bit).

You can stop it failing, and let it use the index if there is one, by doing where some_column > '5' as other have said, or where some_column > to_char(5). But you need to be careful doing the comparison as it will still be a string comparison, not a numeric one; so '10' will not be seen as > '5'; and your NLS sorting parameters might produce results you aren't expecting. Or more importantly, someone else's NLS parameters - when you put this live for example - might product results you aren't expecting and which don't match the ones you got in your environment. See the documentation for more.

You should use number columns to hold numeric values, date columns to hold dates, etc., and varchar2 only to hold text values.

you can use to_char function

select * from table where column > to_char(5)

You are missing the table:

select * where FROM tablename column>5

But this only works if column is a number. If not, you can't use >.


To compare strings, you can use LIKE or STRCMP(), check examples of them HERE.


As stated by @Gerrat, you can also use > and < but the types of both sides must be compatible (number with number or text with text). To find more about it, check THIS.

Be aware that in text comparison it will compare each character individually so '11' will be < that '2'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top