Solved

How to get negative values to work with To_Number?

Posted on 2007-12-04
8
1,209 Views
Last Modified: 2011-10-03
I have the following code snippet. It works when the value in the variable TotalDue is 0 or greater. However if TotalDue has a negative value, it returns 0 instead of the correct value. For instance, if TotalDue is 100 and Sum_Memo is 50, it will return 50. However, if TotalDue is -100, then instead of returning -150, it returns 0. TotalDue and SumMemo are string variables. How can I fix it so it will show the correct value. incidentally, the colon in front of the variable just indicates that it an external variable, it does not have any bearing on the final result.

SELECT
To_Char(NVL(To_Number(:TotalDue),0) - NVL(To_Number(:Sum_Memo),0),'999,999.99')
INTO :TotalDuePrint
FROM dual

Open in new window

0
Comment
Question by:geeta_m9
  • 5
  • 3
8 Comments
 
LVL 13

Expert Comment

by:anand_2000v
Comment Utility
try
SELECT

To_Char(NVL(To_Number(:TotalDue),0) - NVL(To_Number(:Sum_Memo),0),'MI999,999.99')

INTO :TotalDuePrint

FROM dual

Open in new window

0
 

Author Comment

by:geeta_m9
Comment Utility
I cannot use that format, I obtain an "Invalid Number Format Model."
0
 
LVL 13

Expert Comment

by:anand_2000v
Comment Utility
What do you get if you try
SELECT

NVL(To_Number(:TotalDue),0) - NVL(To_Number(:Sum_Memo),0)

INTO :TotalDuePrint

FROM dual

Open in new window

0
 

Author Comment

by:geeta_m9
Comment Utility
The value in the TotalDue has commas in it, i.e. 2,000 and I believe that is what is affecting the output. How do  I strip out the commas from TotalDue using SQL.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 13

Expert Comment

by:anand_2000v
Comment Utility
try
SELECT
NVL(To_Number(replace(:TotalDue,',')),0) - NVL(To_Number(:Sum_Memo),0)
INTO :TotalDuePrint
FROM dual
0
 

Author Comment

by:geeta_m9
Comment Utility
That works! Thank you! However, I need to get it in the format 9,999.99. How can I include that in the statement?
0
 
LVL 13

Expert Comment

by:anand_2000v
Comment Utility
That should work now.
0
 
LVL 13

Accepted Solution

by:
anand_2000v earned 500 total points
Comment Utility

SELECT

To_Char(NVL(To_Number(replace(:TotalDue,',')),0) - NVL(To_Number(replace(:Sum_Memo,',')),0),'999,999.99')

INTO :TotalDuePrint

FROM dual

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example, show how to take different types of Oracle backups using RMAN.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now