Calculating Mathematical Values in SQL Server
By: Jeremy Kadlec | Updated: 2008-07-24 | Comments (16) | Related: More > T-SQL
Free MSSQLTips Webinar: Development Best Practices for SQL Server
Problem
In our application we have the need to perform mathematical calculations. Right now we are doing so in our front end application. Unfortunately we are starting to experience performance problems with large data sets and differences in calculations due to developers using different logic. We are seeking some other options to perform the calculations. Does SQL Server perform basic mathematical calculations?
Solution
Yes - SQL Server can perform basic addition, subtraction, multiplication and division. So if you need some of those basic building blocks those are available and we will walk through some examples in this tip. In addition, SQL Server can calculate SUM, COUNT, AVG, etc. For these type of calculations, check out SQL Server T-SQL Aggregate Functions. To address the multiple code issues, I would recommend researching stored procedures. This tip Getting started with SQL Server stored procedures is probably a good place to start.
In this tip, let's focus on some basic building blocks to perform your calculations with the T-SQL language. Here are the examples we will cover:
- Calculations on Values
- Calculations on Parameters
- Calculations on Columns
- Calculations in Computed Columns
Calculations on Values
As a starting point, values themselves can have mathematical operations performed on them. So in the code below we are performing addition (+), subtraction (-), multiplication (*), division (/) and a combination of operations. In these examples, both positive and negative whole numbers and decimals are used. The value calculated by the statement is below the code to serve as a point of reference. Review and copy the code into a SQL Server 2005 Management Studio window to get a feel for these calculations. Certainly this list is not comprehensive, so feel free to test some operations in your applications or from your experiences.
Calculations on Values |
-- Example 1 - Addition (+)
SELECT 1 + 1 SELECT 1 + 2 + 3 + 4 + 99 + 704 SELECT 1.5 + 1.5 SELECT .25678 + .00096356 SELECT 1.75 + -2.25 -- Example 2 - Subtraction (-) SELECT 1 - 1 SELECT 918 - 704 SELECT 3.2 - 1.9 SELECT 1.9 - 3.2 SELECT 9 - 3 - 3 SELECT .75 - .68 -- Example 3 - Multiplication (*) SELECT 1 * 1 SELECT 2 * -4 SELECT 2 * 5 * 10 SELECT 1.25 * 3 SELECT .4 * .5 -- Example 4 - Division (/) SELECT 1/2 SELECT 1.0/2.0 SELECT 0/5 SELECT 100/12 SELECT 100.0/12.0 SELECT -75.0/4.5 SELECT .5/.1 -- Example 5 - Combination SELECT ((100 + 100) * .05) SELECT (10 - 5)/2 SELECT (10.0 - 5.0)/2.0 SELECT ((100 + 100) - (50 + 50)) |
Calculations on Parameters
Since we have covered the bulk of the calculations in the section above let's just focus on a few different operations to show how parameters with specific data types play an important role. Since this set of examples are not comprehensive, feel free to copy the code below into a SQL Server 2005 Management Studio window and test the code with some of your own logic.
Calculations on Parameters |
-- Variable declaration DECLARE @i1 int DECLARE @i2 int DECLARE @i3 int DECLARE @d1 decimal(10,2) DECLARE @d2 decimal(10,2) DECLARE @d3 decimal(10,2) -- Initialize variables -- Example 1 - Addition (+) SELECT @d2 + -@d3 -- Example 2 - Subtraction (-) SELECT @d2 - @d3 -- Example 3 - Multiplication (*) SELECT @d2 * @d3 -- Example 4 - Division (/) SELECT @d2 / @d3 -- Example 5 - Combination SELECT ((@i1 + @i2) - (@d1 + @d2)) |
Calculations on Columns
Another option is to calculate the values based on a SELECT statement as the example below shows. If the data is in 1 or more columns, this approach eliminates the need to calculate the values based on additional parameters or logic.
Calculations on Columns |
-- Sample Table CREATE TABLE dbo.CalculationExample( ProductID int NOT NULL, Cost decimal(10,2) NOT NULL) GO -- Populate Table -- Verify Insertion -- Declare Variables -- Initialize Variables -- Calculate Values |
Calculations in Computed Columns
Let's take those same calculations and now move them to computed columns in the table. Here is that example:
Calculations in Computed Columns |
CREATE TABLE [dbo].[CalculationExample_ComputedColumns]( [ProductID] [int] NOT NULL, [Cost] [decimal](18, 0) NOT NULL, [Tax] AS ([Cost]*(0.05)), [Margin] AS ([Cost]*(0.20)), [FinalCost] AS (([Cost]+[Cost]*(0.20))+[Cost]*(0.05)), CONSTRAINT [PK_CalculationExample_ComputedColumns] PRIMARY KEY CLUSTERED ( [ProductID] ASC )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] -- Populate Table -- Review the values |
Next Steps
- Review the code in each of these sections and test them out for yourself to get a feel for these types of operations.
- Review your application and think about different components that require calculations to determine which option best meets the needs.
- If you are faced with changing, improving or consolidating calculations, consider the options in this tip as potential options to address those needs.
- Check out these related tips:
Last Updated: 2008-07-24
About the author
View all my tips