Oracle - select * where column>5

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?
Muchas Gracias

preguntado el 31 de julio de 12 a las 14:07

5 Respuestas

Yes, you have to put the 5 in quotes :

select * from table where column > '5'

Respondido 31 Jul 12, 14:07

Bear in mind that when comparing strings '5' > '40' is verdadero. This is why it is generally a bad idea to store numeric data as a string. - APC

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.

Debes nunca 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 inferior than the (character) value '2' because the first character '1' is ranked lower than '2'.

Respondido 31 Jul 12, 15:07

Si la columna es varchar2, luego esto:

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

Es el 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' detectarás no ser visto como > '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. Consulte la documentación para obtener más.

Deberías usar number columns to hold numeric values, date columns to hold dates, etc., and varchar2 only to hold text values.

Respondido 31 Jul 12, 15:07

puedes usar to_char función

select * from table where column > to_char(5)

Respondido 31 Jul 12, 15:07

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 AQUÍ.

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

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

Respondido 31 Jul 12, 15:07

You can use comparisons like '>' with strings in Oracle. It does what you'd expect. - Gerrat

@Gerrat: well not necessarily what you'd expect, because '10' is menores than '2' - un caballo sin nombre

@a_horse_with_no_name: Well, it might not be what algo people expect, but I'd wager that aF would expect a lexical comparison :) - Gerrat

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.