------------------------------------------------------------------------------------------------------------------------------------------
TITLE: A Simple Tip for Calculating Percentages
CANONICAL URI: https://www.totalsql.com/sql-server-content/tips/a-simple-tip-for-calculating-percentages
AUTHOR: Michael K. Campbell 
COPYRIGHT: 2026 All Rights Reserved: OverAchiever Productions, LLC. 
RELEASED: 2024-09-04
------------------------------------------------------------------------------------------------------------------------------------------

# A Simple Tip for Calculating Percentages

## A Simple Tip
This tip won't win any awards, usher in world peace, or even spare you from hours of tedium. 

But it does leverage some subtle details about data-type conversions, and can make calculating percentages with integers a bit easier - and more precise.

To get a sense for what I'm going on about, take a look at **Figure 1**, below:

<figure class="figure">
  <img src="https://assets.overachiever.net/ts/tips/calculating_percentages_1.gif" class="figure-img img-fluid" alt="Basic Percentage Calculation - against canned values.">
  <figcaption class="figure-caption"><b>FIGURE 1:</b> Moving the 100 'up top' requires less conversion (and yields better precision).</figcaption>
</figure>

On **Line #3**, you can see the normal, documented, and expected outcome of attempting to divide smaller integers by each other. 

On **Line #5**, I've used some simple short-hand to convert my integers to `decimal` (or `numeric`) data-types by merely 'plunking' a decimal 'place-holder' on to the end of each of the values from **Line #3**. What's interesting, though, is that the results show a loss of precision. 

Then, on **Line #7**, all that's happening is that I'm showing that you can use the formula `partial_value * 100.0 / total_value` as a means of obtaining a percentage vs the typical or conventional approach which is to use `(partial_value / total_value) * 100`. 

Note too, that - interestingly enough - we've regained some precision with this approach - simply because we've got a larger dividend (i.e., `partial * 100.0` is a much larger number to divide than what you get when dividing `partial \ total`). 

Finally, a snapshot of my entire 'tip' is found on **Line #9** - where I revert both the `partial` and `total` 'parts' of the perentage back into to their 'natural' integer types, and manage to still get a fully-precise and 100% viable percentage by MERELY a) using my `partial * 100.0 / total` 'formula' and by b) making sure that the `100.0` is of `numeric` or `decimal` data-type (which, in turn, coerces the other values in this equation to be of the same data type).

Again, this won't win any awards, or even save you gobs of time. 

But, when it's used against actual TABLE data (vs static (pre-known) values), this tip can save you a decent amount of cognitive clutter by decreasing the number CAST/CONVERT operations you'd otherwise need to employ - as can be seen from the following screenshot: 

<figure class="figure">
  <img src="https://assets.overachiever.net/ts/tips/calculating_percentages_2.png" class="figure-img img-fluid" alt="Examples of the tip in action.">
  <figcaption class="figure-caption"><b>FIGURE 2:</b> The code highlighted in green is simpler to read (and type) AND yields better precision.</figcaption>
</figure>

Or, feel free to take the code for a spin yourself: 

```sql

DECLARE @integers table ( 
	partial_value int NOT NULL, 
	total_value int NOT NULL 
); 

INSERT INTO @integers ([partial_value], [total_value])
VALUES
(10, 100), 
(3, 9), 
(54, 267);

SELECT 
	CAST([partial_value] AS decimal(10,2)) 
	/ 
	CAST([total_value] AS decimal(10,2)) * 100.0 [traditional]
FROM 
	@integers;

SELECT 
	[partial_value] * 100.0 / [total_value] [simpler]
FROM 
	@integers;
	
```

Either way, you'll see that `partial_value * 100. / total_value` is a simple trick that decreases the need for CAST()/CONVERT() operations, while even frequently yielding BETTER precision.

