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
 how to compute a percentage in sql?

Author  Topic 

Trininole
Yak Posting Veteran

83 Posts

Posted - 2010-02-24 : 09:11:19
what is the syntax for calculating a percentage in sql?

Roger DeFour

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-24 : 09:16:35
quote:
Originally posted by Trininole

what is the syntax for calculating a percentage in sql?

Roger DeFour


There is no built-in function. You need to write

value1/value2*100

Madhivanan

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

Sachin.Nand

2937 Posts

Posted - 2010-02-24 : 09:24:42
[code]
declare @value1 MONEY=50
declare @value2 money=10
select convert(VARCHAR,@value1 * @value2 / 100) + '%'
[/code]

PBUH
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-24 : 09:27:28
make sure you use 100.0 if operands are of type integer if you need to get decimal part in result.

value1/value2*100.0

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-24 : 09:30:02
reason is this

http://beyondrelational.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -