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
 General SQL Server Forums
 New to SQL Server Programming
 Percent - 2 Decimal

Author  Topic 

cutiebo2t
Constraint Violating Yak Guru

256 Posts

Posted - 2008-05-02 : 09:12:34
Hi,

How can I convert my number results to Percent (%) with 2 decimal places?

Thanks

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-02 : 09:13:44
Post full sample data and expected output.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

cutiebo2t
Constraint Violating Yak Guru

256 Posts

Posted - 2008-05-02 : 09:16:25
select* sum(data)/count(data) as results
From Data
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-05-02 : 09:21:14
quote:
Originally posted by cutiebo2t

select* sum(data)/count(data) as results
From Data



select cast(sum(data)/count(data)*1.0 as decimal(12,2)) as results
From Data


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

cutiebo2t
Constraint Violating Yak Guru

256 Posts

Posted - 2008-05-02 : 09:31:35
thanks but can it be in percentage form. Example 34.56%.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-05-02 : 09:37:41
quote:
Originally posted by cutiebo2t

thanks but can it be in percentage form. Example 34.56%.


That should be shown in your front end application

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

cutiebo2t
Constraint Violating Yak Guru

256 Posts

Posted - 2008-05-02 : 09:38:52
oh i see. So it's not possible with SQL?
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-02 : 09:40:18
It is possible but not advisable to do in SQL.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

cutiebo2t
Constraint Violating Yak Guru

256 Posts

Posted - 2008-05-02 : 09:43:34
may i know how? thanks
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-05-02 : 09:58:15
quote:
Originally posted by cutiebo2t

may i know how? thanks


You have to convert the value to varchar and it is not possible to use that value in any arithmetic calculations

Where do you want to show data?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2008-05-03 : 05:34:23
quote:
Originally posted by cutiebo2t

thanks but can it be in percentage form. Example 34.56%.



select cast(cast(sum(data)/count(data)*1.0 as decimal(12,2))as varchar(50))+'%' as results
From Data
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-03 : 06:05:24
quote:
Originally posted by raky

quote:
Originally posted by cutiebo2t

thanks but can it be in percentage form. Example 34.56%.



select cast(cast(sum(data)/count(data)*1.0 as decimal(12,2))as varchar(50))+'%' as results
From Data


Better to do this formatting at your front end application
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-05-03 : 07:13:45
quote:
Originally posted by raky

quote:
Originally posted by cutiebo2t

thanks but can it be in percentage form. Example 34.56%.



select cast(cast(sum(data)/count(data)*1.0 as decimal(12,2))as varchar(50))+'%' as results
From Data


How do I add 10 to that result?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -