Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Percentage problem converting decimal values 1

Status
Not open for further replies.

beetlebailey

Programmer
Jan 4, 2005
51
US
Trying to use the following SQL query:
Select Hour1/cast((Tact1 as decimal)*100) as PCT1 or
Select Hour1/cast((Tact1 as float)*100) as PCT1
both of which return 76.923076923076934

My question is why cannot I get rid of the extra decimal points ? I would like it to be just 76.92%

 
You never specify the amount of digits to use. That's like using VARCHAR instead of VARCHAR(10).

Try DECIMAL(4,2)

-SQLBill

Posting advice: FAQ481-4875
 
Use the ROUND function.

SELECT Round(Hour1/cast((Tact1 as decimal)*100),-2) as PCT1
FROM ...

--John [rainbow]
-----------------------------------
Behold! As a wild ass in the desert
go forth I to do my work.
--Gurnie Hallock (Dune)
 
Select Hour1/cast(Tact1 as decimal(4,2))*100 as PCT1
yields the following results: 76.923000
still would like to get rid of the 3000
Any ideas ?
Thanks

 
Can you provide examples of Hour1 values?

I think that's where the issue is.

You might even test this:

SELECT CAST(Hour1/(CAST Tact1 as DECIMAL(4,2)) AS DECIMAL(4,2))* 100
FROM ....

and see what that returns.

-SQLBill

Posting advice: FAQ481-4875
 
I think you should use Decimal(5,2). The largest number that Decimal(4,2) can hold is 99.99. 100% is a valid number. Decimal(5,2) would prevent an arithmetic overflow error.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top