# adding a percent

 0 I am trying to get a percent from 2 fields ie add [A]+[B]/[A]*100 AS [C] to get my percent. if i try [A]/sum([A]+[B])*100 it returns a 0 and if i try sum([A]+[B])/[A]*100 i get a diide by zero error. values are [A] 3 [b] 1 percent should be 75. Thanks for any help on this more ▼ asked May 29, 2012 at 11:10 AM in Default n3w2sql 850 ● 14 ● 25 ● 33 add new comment (comments are locked) 10|1200 characters needed characters left ▼ Viewable by all users

1 answer: sort voted first
 0 It's because this is doing integer division, so 3/sum(3+1) = 0You need to convert the top line into a decimal, such as (3 * 1.0)/sum(3+1) = 0.75This makes your query (([A]*1.0)/([A]+[B]) * 100) more ▼ answered May 29, 2012 at 11:15 AM Kev Riley ♦♦ 53.9k ● 47 ● 49 ● 76 Thanks Kev that works perfect, I have really been struggling with percentages but I have a much better understanding now with your help. cheers May 30, 2012 at 09:03 AM n3w2sql add new comment (comments are locked) 10|1200 characters needed characters left ▼ Viewable by all users

### New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1850

asked: May 29, 2012 at 11:10 AM

Seen: 319 times

Last Updated: May 30, 2012 at 09:05 AM