I have a problem with the query I have a table newtable and have inserted values:
create table newtable(
col1 varchar2(3),
col2 varchar2(3),
col3 varchar2(3),
col4 varchar2(3),
col5 varchar2(3)
)
insert into newtable values(5,'m','q',4,3);
I want to add together the values in the columns which have only digits. In this case, I want the result to be 12 (5+4+3).
In Excel, I would do something similar with the countif function. How do I do this in Oracle?
You could create a function that either converts the string to a number or returns a 0 and use that function on each column
In a relational database, however, it is almost certainly a mistake to design a data model where you are storing numeric data in a
varchar2column when you want to subsequently treat that data as a number to do things like add the numbers together. It is almost certainly a mistake to design a data model where a particular column will sometimes store a numeric value and sometimes store a string. So I would be exceptionally concerned that the design ofnewtableneeds to be rethought.