Home » SQL & PL/SQL » SQL & PL/SQL » Summation on Character Variables (Oracle 10g)
Summation on Character Variables [message #649803] Wed, 06 April 2016 10:28 Go to next message
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 #649805 is a reply to message #649803] Wed, 06 April 2016 10:55 Go to previous messageGo to next message
sandeep_orafaq
Messages: 88
Registered: September 2014
Member
Why do you have character variables holding numbers?
Why are you adding 2 character variables (assuming summarize means addition here)?
What is difference between two numbers(not characters) 30.3 and 30.30?
Re: Summation on Character Variables [message #649806 is a reply to message #649805] Wed, 06 April 2016 11:05 Go to previous messageGo to next message
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 #649807 is a reply to message #649806] Wed, 06 April 2016 11:06 Go to previous messageGo to next message
chat2raj.s
Messages: 161
Registered: October 2010
Location: Chennai, India
Senior Member
I meant the sum of decimal portion reached 45, i will have increase the integer by 1.
Re: Summation on Character Variables [message #649808 is a reply to message #649807] Wed, 06 April 2016 11:11 Go to previous messageGo to next message
sandeep_orafaq
Messages: 88
Registered: September 2014
Member
still there is no difference between 0.3 and 0.30

and there is difference between 0.03 and 0.3 Smile

If you are doing whatever you are trying to do mathematically(using numbers) 0.3 and 0.30 are the same.
Re: Summation on Character Variables [message #649809 is a reply to message #649807] Wed, 06 April 2016 15:59 Go to previous messageGo to next message
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 #649813 is a reply to message #649809] Thu, 07 April 2016 00:17 Go to previous messageGo to next message
chat2raj.s
Messages: 161
Registered: October 2010
Location: Chennai, India
Senior Member
Thank you. I will rework to change my design. Till then i can use this code. Thanks Barbara and Sandeep.
Re: Summation on Character Variables [message #650009 is a reply to message #649813] Tue, 12 April 2016 09:59 Go to previous messageGo to next message
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

Re: Summation on Character Variables [message #650026 is a reply to message #650009] Wed, 13 April 2016 00:21 Go to previous message
chat2raj.s
Messages: 161
Registered: October 2010
Location: Chennai, India
Senior Member
I will use your solution time being. Thanks Bill.
Previous Topic: How to create Dependency in MV
Next Topic: Stored Procedure: XML versus Array for multi-table insert
Goto Forum:
  


Current Time: Wed Apr 24 11:41:26 CDT 2024