Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Convert to percentage

Author  Topic 

jung1975
Aged Yak Warrior

503 Posts

Posted - 2005-02-09 : 11:08:08
How can I convert the number with decimal points to percentage?

For example,

0.2345 --> 20%
0.89768 --> 90%
0.346578 --> 30%





AndyB13
Aged Yak Warrior

583 Posts

Posted - 2005-02-09 : 11:16:06
SELECT CONVERT(int,ROUND(0.2345 * 100,-1)) --> 20%

SELECT CONVERT(int,ROUND(0.89768 * 100,-1)) --> 90%

SELECT CONVERT(int,ROUND(0.346578 * 100,-1)) --> 30%

Andy
Go to Top of Page

jung1975
Aged Yak Warrior

503 Posts

Posted - 2005-02-09 : 11:56:46
How about something like this?

0.2345 --> 0.2
0.89768 --> 0.9
0.346578 --> 0.3



Go to Top of Page

AndyB13
Aged Yak Warrior

583 Posts

Posted - 2005-02-09 : 12:08:47
Look up CONVERT/CAST & ROUND in BOL

Andy
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-02-10 : 05:49:07

SELECT substring(convert(varchar(10),ROUND(0.2345+0.05,2)),1,3)

Madhivanan
Go to Top of Page

AndyB13
Aged Yak Warrior

583 Posts

Posted - 2005-02-10 : 06:06:10
madhivanan
I had already give jung1975 the answer to the original question which is why i pointed him in the direction of BOL when the same question was asked again but with a different output.

However your solution doesnt work
quote:

How about something like this?

0.2345 --> 0.2
0.89768 --> 0.9
0.346578 --> 0.3


Using your solution
SELECT substring(convert(varchar(10),ROUND(0.2345+0.05,2)),1,3) --0.2
SELECT substring(convert(varchar(10),ROUND(0.89768+0.05,2)),1,3) --0.9
SELECT substring(convert(varchar(10),ROUND(0.346578+0.05,2)),1,3) --0.4 (incorrect)


Another way
SELECT CONVERT(varchar(10),CONVERT(decimal(10,1),ROUND(0.2345,1))) --0.2
SELECT CONVERT(varchar(10),CONVERT(decimal(10,1),ROUND(0.89768,1))) --0.9
SELECT CONVERT(varchar(10),CONVERT(decimal(10,1),ROUND(0.346578,1))) --0.3

Andy
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-02-10 : 06:43:06

Thanks for your suggestion Andy

Madhivanan
Go to Top of Page
   

- Advertisement -