I have a varchar column in oracle DB that stores numeric and character values.
ID : VALUE
01 : 12
02 : 15
03 : 16
04 : 08
05 : Positive
I got error when I used numeric condition in where clause.
WHERE VALUE > 10
How can i fix this situation?
I have a varchar column in oracle DB that stores numeric and character values.
ID : VALUE
01 : 12
02 : 15
03 : 16
04 : 08
05 : Positive
I got error when I used numeric condition in where clause.
WHERE VALUE > 10
How can i fix this situation?
If you are on Oracle 12cR2 then you can use default value
when conversion error happens.
SQL>
SQL> CREATE TABLE YOUR_TABLE AS
2 (SELECT '01' AS ID , '12' AS VALUE FROM DUAL UNION ALL
3 SELECT '02', '15' FROM DUAL UNION ALL
4 SELECT '03', '16' FROM DUAL UNION ALL
5 SELECT '04', '08' FROM DUAL UNION ALL
6 SELECT '05', 'Positive' FROM DUAL);
Table created.
SQL>
SQL> SELECT * FROM YOUR_TABLE
2 where TO_NUMBER(value DEFAULT -1 ON CONVERSION ERROR) > 10;
ID VALUE
-- --------
01 12
02 15
03 16
Cheers!!
As you stated, your value
column consists of character values.
To apply correct numeric condition on it, you have to turn it into number:
SELECT *
FROM table
WHERE 10 < CASE value
WHEN 'Positive' THEN null
WHEN 'Negative' THEN null
ELSE TO_NUMBER(value)
END