Avatar of csharp_learner
csharp_learner
Flag for Singapore asked on

Trailing 0 got dropped

Hi,

I am trying to subtract a value from a column ADDRESS by using:
select NAME,ADDRESS,ADDRESS-7 from entity where ....

The result returned with the trailing 0 being dropped.
eg: ADDRESS, ADDRESS-7
154.10      147.1
154.1      147.1
154.15      147.15
155.0      148

How can I keep the trailing 0s?

PS: I am using TOAD version 10.6.1.3 as the browser and the ADDRESS column is created as VARCHAR2.
Oracle DatabaseDatabases

Avatar of undefined
Last Comment
Sean Stuber

8/22/2022 - Mon
Sean Stuber

when you do math on the address field, the result is implicitly converted to a NUMBER type which will display in simplest form.

if you want formatted results, then you need to return the math result as text


select NAME,ADDRESS,to_char(ADDRESS-7,'999999.00') from entity where ....
Peter Chan

Put
col res format 999,999.00
select NAME,ADDRESS,ADDRESS-7 res from entity where ....

Open in new window

csharp_learner

ASKER
Thanks for the prompt reply:
@sdstuber: The results is all double decimal points
154.10    147.10
154.1      147.10
154.15    147.15
155.0      148.00

Which is not the result i'm looking for.

@HuaMinChen: There is a SQL format error in the statment.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
csharp_learner

ASKER
Just to clarify the results i would like to achieve is:

154.10    147.10
154.1      147.1
154.15    147.15
155.0      148.0
Peter Chan

Do you put "res" as the column name? what is the error?
csharp_learner

ASKER
@HuaMinChen: the error is Invalid SQL Statement.

I ran the exact same statement:
col res format 999,999.00
select NAME,ADDRESS,ADDRESS-7 res from entity where ....

and tried this also witht the same error
col res format 999,999.00
select NAME,ADDRESS,ADDRESS-7 as "res" from entity where ....
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Peter Chan

Try
col res format 999,999.00
select NAME,ADDRESS,ADDRESS-7 as res from entity where ....

Open in new window

Peter Chan

Not to put "res"
Sean Stuber

the "col" syntax is for sqlplus

Toad can mimic the results but only when running as script.

to get the formatted results in the data grid, you'll need to use to_char in the query
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Sean Stuber

to get the same number of decimals as the original text  try this...

SELECT name,
       address,
       CASE
           WHEN INSTR(address, '.') > 0
           THEN
               TO_CHAR(
                   address - 7,
                   'fm9999.' || RPAD('0', LENGTH(SUBSTR(address, INSTR(address, '.') + 1)), '0')
               )
           ELSE
               TO_CHAR(address - 7)
       END x


if there will always be a decimal point, then it can be simplified by removing the CASE


SELECT name,
       address,
       TO_CHAR(
           address - 7,
           'fm9999.' || RPAD('0', LENGTH(SUBSTR(address, INSTR(address, '.') + 1)), '0')
       ) x
  FROM entity


  FROM entity
awking00

Similarly with explicit conversions -
select address,
case when length(address) - instr(address,'.') = 2 then to_char(to_number(address) - 7,'9999999.00')
     when length(address) - instr(address,'.') = 1 then to_char(to_number(address) - 7,'9999999.0')
     else to_char(to_number(address) - 7,'9999999')
end addminus7
from entity;
slightwv (䄆 Netminder)

Can it not be simplified a little?

I'm thinking the address column must be varchar2 or it wouldn't have the trailing '0' to start with.

Just subtract 7 from the whole number and tack on everything after the decimal?


select floor(to_number(address))-7 || regexp_substr(address,'\..+$') from entity;
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Sean Stuber

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.