Summation on Character Variables [message #649803] |
Wed, 06 April 2016 10:28 |
chat2raj.s
Messages: 161 Registered: October 2010 Location: Chennai, India
|
Senior Member |
|
|
Dears,
I have 2 character variable having 15.10 and 15.20. When i try to summarize them, it results as 30.3 instead of 30.30. To_number suppresses the trailing zeroes and it is not helping.
Kindly suggest.
Thanks.
|
|
|
|
Re: Summation on Character Variables [message #649806 is a reply to message #649805] |
Wed, 06 April 2016 11:05 |
chat2raj.s
Messages: 161 Registered: October 2010 Location: Chennai, India
|
Senior Member |
|
|
Somehow user has to enter some quantity in a character field (Which is not the original quantity field and an extra flex field which is of character type). I need the total quantity entered, where it fails giving the value 30.30. Technically when the sum reaches 45, i need to increase the integer by 1. So the value 3 and 30 has a difference for me in this current situation.
|
|
|
|
|
Re: Summation on Character Variables [message #649809 is a reply to message #649807] |
Wed, 06 April 2016 15:59 |
|
Barbara Boehmer
Messages: 9090 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
It is a bad design, but if, for some reason, you cannot change it, then the following will derive what you want. It separates the parts before and after the period, then implicitly converts them to numbers as it does the calculations, then implicitly converts them back to characters as it concatenates them.
SCOTT@orcl> column summation format a15
SCOTT@orcl> with
2 test_data as
3 (select '15.10' var1, '15.20' var2 from dual
4 union all
5 select '15.10' var1, '15.40' var2 from dual)
6 select var1, var2,
7 int+trunc(dec/45)||'.'||mod(dec,45) summation
8 from (select var1, var2,
9 trunc(var1)+trunc(var2) int,
10 substr(var1,instr(var1,'.')+1)+substr(var2,instr(var2,'.')+1) dec
11 from test_data)
12 /
VAR1 VAR2 SUMMATION
----- ----- ---------------
15.10 15.20 30.30
15.10 15.40 31.5
2 rows selected.
|
|
|
|
Re: Summation on Character Variables [message #650009 is a reply to message #649813] |
Tue, 12 April 2016 09:59 |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
If you can't change the table, I would take 2 actions. First add a check constraint on the column so that bad data can't be entered in. Two change your select to format your data correctly. The regular expression below will allow any number with our without a period to be entered. A sign "-" must be entered first (-123.456)
ALTER TABLE MY_TABLE ADD (
CONSTRAINT BAD_NUMBER
CHECK (regexp_like(my_qty,'^-?\d*\.?\d*$'))
ENABLE VALIDATE);
select to_char(sum(to_number(my_qty)),'FM9999999999990.00') FROM MY_TABLE;
before you add the constraint, you can check the data by running the following query
select * from my_table
where not regexp_like(my_qty,'^-?\d*\.?\d*$');
The regular expression allows nulls in the column. If you don't want nulls, make the column not null;
[Updated on: Tue, 12 April 2016 12:51] Report message to a moderator
|
|
|
|