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 '12 at 11:10 AM in Default n3w2sql 840 ● 12 ● 20 ● 26 add new comment (comments are locked) 10|1200 characters needed characters left ▼ Viewable by all users

 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 '12 at 11:15 AM Kev Riley ♦♦ 50.8k ● 43 ● 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 '12 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.

By Email:

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

Topics:

x1816

asked: May 29 '12 at 11:10 AM

Seen: 284 times

Last Updated: May 30 '12 at 09:05 AM