Home » SQL & PL/SQL » SQL & PL/SQL » Select query given ORA-01722 error message (ora 11g)
Select query given ORA-01722 error message [message #603477] Mon, 16 December 2013 22:08 Go to next message
Tlg13team
Messages: 100
Registered: June 2008
Location: MGL
Senior Member
Hi all,

a) SELECT * FROM TABLEA WHERE COL1 = 100;

Above query cannot run today given "ORA-01722: invalid number" error message but it's working fine long time before.
When I add "'" symbol in where condition, it's working fine like below.

b) SELECT * FROM TABLEA WHERE COL1 = '100';

"col1" field type was varchar2.

Can you explain it?
First query still working fine on my test server.

I saw only a difference for this column in table between PROD and TEST database.
When I see table colums by Toad, Historgam="Hieght Balanced" on PROD but Historgam="None" on TEST database for that COL1 of TABLEA.
Re: Select query given ORA-01722 error message [message #603479 is a reply to message #603477] Mon, 16 December 2013 22:19 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Oracle is too dumb to lie about error.
I believe that Oracle better reports reality than you.

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/
Re: Select query given ORA-01722 error message [message #603496 is a reply to message #603477] Tue, 17 December 2013 00:20 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

ORA-01722: invalid number
 *Cause: The specified number was invalid.
 *Action: Specify a valid number.

The first does not work because your column which is of string type, contains a not number and "COL1 = 100" is converted to "TO_NUMBER(COL1) = 100".

The second one "works" because it compares strings and so there is no NUMBER.



Re: Select query given ORA-01722 error message [message #603639 is a reply to message #603496] Wed, 18 December 2013 02:44 Go to previous message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Hi, I'll try to explain it a little bit further: When you compare two different data types, Oracle will implicitly cast one of the two sides of the equation. In your case, Oracle will try to convert COL1 to a number. If all values in COL1 can be converted to a number, you're fine but if one value can't be casted to a number, you'll hit the error you encountered. It is good practice to make sure that Oracle doesn't apply implicit casting.

Here's a small demonstration. Look at the difference in the data. The first version contains only numeric values.

-- This will work
With yourtable
AS
( Select cast('1' as varchar2(1)) char_column, 'One'   description From dual Union All
  Select cast('2' as varchar2(1)) char_column, 'Two'   description From dual Union All
  Select cast('3' as varchar2(1)) char_column, 'Three' description From dual Union All
  Select cast('4' as varchar2(1)) char_column, 'Four'  description From dual Union All
  Select cast('5' as varchar2(1)) char_column, 'Five'  description From dual Union All
  Select cast('6' as varchar2(1)) char_column, 'Six'   description From dual Union All
  Select cast('7' as varchar2(1)) char_column, 'Seven' description From dual
)
Select *
From   yourtable
Where  char_column = 2
/

-- This won't work at all:
With yourtable
AS
( Select cast('A' as varchar2(1)) char_column, 'One'   description From dual Union All
  Select cast('2' as varchar2(1)) char_column, 'Two'   description From dual Union All
  Select cast('B' as varchar2(1)) char_column, 'Three' description From dual Union All
  Select cast('4' as varchar2(1)) char_column, 'Four'  description From dual Union All
  Select cast('5' as varchar2(1)) char_column, 'Five'  description From dual Union All
  Select cast('6' as varchar2(1)) char_column, 'Six'   description From dual Union All
  Select cast('7' as varchar2(1)) char_column, 'Seven' description From dual
)
Select *
From   yourtable
Where  char_column = 2
/


MHE
Previous Topic: How to add and multiply values in columns
Next Topic: Problem in SQL report
Goto Forum:
  


Current Time: Thu Apr 25 05:48:43 CDT 2024