Open In App

SQL Server Arithmetic Operators

Last Updated : 24 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Microsoft’s SQL Server, a capable relational database management system, has a lot of features to deal with data and make manipulations purely. SQL Server, in addition to the many features available to users, has a set of operators for arithmetic which allows users of databases to perform mathematical operations on data stored within tables.

In this article, SQL Server arithmetic operators will be explored, including how to use them, their syntax, and the context of practical examples.

Introduction to SQL Server Arithmetic Operators

Arithmetic operators in SQL servers allow for performing basic calculations like addition, subtraction, multiplication, division, and modulo (remainder). These operators may do operations like keyword arithmetic and floating number points that are stored in columns of SQL Server tables.

Common Arithmetic Operators in SQL Server

  1. Addition (+): The addition operation is shown by a plus symbol “+”, and it is used to combine two numeric values. It can be used for binding data with semi-characters in doing data VARCHAR typing.
  2. Subtraction (-): The minus sign (-) is used to denote the subtraction operator, and it removes the numeric value from another(s). It is used in noticing the difference or the change between two values, for example, if you calculate the variance between your weekly sales and your targeted value.
  3. Multiplication (*): The asterisk (*) within the operation is used for the multiplication of two numeric values. This operator is reasonably flexible as it not only can be used to capture continuous sequences but also when used with integer values.
  4. Division (/): The dividing symbol is a forward slash (/) and is used to divide two numeric values. You should note that the division by zero results in an error. With integer division SQL Server returns truncated result to the nearest integer because of rounding down the remainder.
  5. Modulo (%): Modulo operation, represented by % (percent sign), calculates the remainder of a result after dividing it. Generally, this kind of algorithm is based on those cases, mainly such as categorization and grouping according to an existing pattern’s copies.

Examples of SQL Server Arithmetic Operators

CREATE TABLE Sales (
OrderID INT,
Product VARCHAR(50),
Quantity INT,
UnitPrice DECIMAL(10, 2)
);


INSERT INTO Sales (OrderID,Product, Quantity, UnitPrice);
VALUES
(1, 'Laptop', 2, 1000.00),
(2, 'Smartphone', 3, 700.00),
(3, 'Headphones', 5, 50.00);

Output:

TABLE

Example of Addition Operators

SELECT OrderID, Quantity + 1 AS Increased_Quantity
FROM Sales;

Output:

ADDITION-OPERATOR

Explanation: This query retrieves the “OrderID” and increases the “Quantity” of each order by 1. The result set shows the order ID alongside the incremented quantity for each sale item.

Example of Subtraction Operators

SELECT OrderID, UnitPrice - 50 AS Reduced_Price
FROM Sales;

Output:

SUBTRACTIONN-OPERATOR

Explanation: This query will decrease the unit price of each product by 50.

Example of Multiplication Operators

SELECT OrderID, Quantity * UnitPrice AS Total_Price
FROM Sales;

Output:

MULTIPLICATE-OPERATOR

Explanation: This query will calculate the total price for each order by multiplying the quantity with the unit price.

Example of Division Operators

SELECT OrderID, UnitPrice / Quantity AS Price_Per_Unit
FROM Sales;

Output:

DIVISION

Explanation: This query will calculate the price per unit for each product by dividing the unit price by the quantity.

Example of Modulus Operators

SELECT OrderID, Quantity % 2 AS Remainder,FROM Sales;

Output:

MODULAS

Explanation: This query will calculate the remainder when dividing the quantity of each product by 2.

Conclusion

The knowledge and mastering of these arithmetic operators in SQL Server are the only things that can help you to perform any complicated act of studying and mathematizing numerical data within your queries. Whether you manipulate financial data, inventory management, or any other domain where the mathematical operations apply, those operators are around your shoulder in your SQL kit.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads