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.


more ▼

asked May 27, 2011 at 08:13 AM in Default

avatar image

1.3k 75 104 147

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

You would want...

 declare @ValueA [int], @ValueB [int]
 SET @ValueA = 112026 
 SET @ValueB = 10078

 SELECT (@ValueB * 100.0 / @ValueA)
more ▼

answered May 27, 2011 at 08:13 AM

avatar image

Matt Whitfield ♦♦
29.5k 62 66 88

  • 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.

May 27, 2011 at 08:16 AM Kev Riley ♦♦

Thank you!

May 27, 2011 at 08:18 AM Slick84
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

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



Answers and Comments

SQL Server Central

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



asked: May 27, 2011 at 08:13 AM

Seen: 1074 times

Last Updated: May 27, 2011 at 08:17 AM

Copyright 2018 Redgate Software. Privacy Policy