SQL Server Arithmetic Operators

SQL Server - Arithmetic Operators

SQL Server supports arithmetic operators such as addition, subtraction, division and multiplication on numeric columns.

  • + ( Addition ): used to perform the add operation on numeric columns.
  • - ( Subtraction ): used to perform the subtract operation on numeric columns.
  • / ( Division ): used to perform the division operation on numeric columns.
  • * ( Multiplication ): used to perform the multiplication operation on numeric columns.
  • % ( Modulo ): used to perform modulo operation on numeric columns.

Arithmetic operators can be used in the SELECT Statement with column names and numeric constants in any combination.

Arithmetic Operators Rules

  • Arithemtic operators can be used on numeric columns or numeric constants.
  • Modulo (%) operator cannot be used with columns of money, smallmoney, float, or real datatypes.

Employee Table

Let us consider the example 'Employee' database table.

Employee table output

Arithmetic Operators Example

Addition operations on SELECT Statement.
SELECT Id, Name, DeptId, Email, Salary + 1000
FROM Employee
Output:
Arithmetic operators output

Arithmetic Operators with Column Heading Example

Addition operations on SELECT Statement to update the salary column with numeric value.
SELECT Id, Name, DeptId, Email, Salary + 1000 'Updated Salary'
FROM Employee
Output:
Arithmetic operators output
SELECT Id, Name, DeptId, Email,UpdatedSalary = Salary + 1000
FROM Employee
Output:
Arithmetic operators output

Arithmetic Operators Precedence

If Multiple arithmetic operators are used in single query, processing operation takes place based on precedence of arithmetic operators.

Precedence level of arithmetic operators in an expression is multiplication (*), division (/), modulo (%), subtraction (-) and addition (+). precedence of arithmetic operators can be changed using parentheses.

SELECT Id, Name, DeptId, Email, (Salary + 1000)+((Salary*20)/100) 'Updated Salary'
FROM Employee
Output:
Arithmetic operators output



Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^