question

Slick84 avatar image
Slick84 asked

Calculating Percentage in SQL

I have a Value A of 112026 and a Value B of 10078. I am dividing Value B by Value A and multiplying it by a 100. On the calculator its fine. When I do this in SQL like so: SELECT (ValueB/ValueA)*100 I constantly get a "0" zero in my result set. Any help to avoid this would be appreciated. Thanks,
S
t-sqlcalculations
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Matt Whitfield avatar image
Matt Whitfield answered
You would want... declare @ValueA [int], @ValueB [int] SET @ValueA = 112026 SET @ValueB = 10078 SELECT (@ValueB * 100.0 / @ValueA)
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

+1 when you just use integers, then you will get an integer result, multiplying one of the values by 1.0, turns it into a numeric data type with decimal places, and the calculation is fine.
3 Likes 3 ·
Thank you!
1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.