Subtraction operator is used to perform subtraction of any numeric and datetime type fields in sql server.
Syntax of - ( Subtract ) Operator :
expression - expression
Expression is any valid expression of the numeric data type except bit data type in sql server.
Example of - ( Subtract ) Operator :
Example 1 : Subtraction of numbers.
Ouput
2
Above example illustrates use of subtraction for numbers.
Example 2 : Display unitinstock by subtracting unit sold from total quantity.
SELECT ProductName, TotalQuantity - UnitSold AS UnitInStock
FROM Product
Output
ProductName UnitInStock
Chai 60
Chang 40
Aniseed Syrup 25
Chef Anton's Cajun Seasoning 46
Chef Anton's Gumbo Mix 30
Grandma's Boysenberry Spread 120
Uncle Bob's Organic Dried Pears 200
Above example explains use of subtraction operator on table fields. UnitInStock displays total quantity available in shop by subtracting UnitSold from TotalQuantity field.
Example 3 : Displays employee who has joined company in last year.
SELECT EmployeeName, HireDate, DAY(SYSDATE - HireDate) AS NumberOfDays
FROM Employee
WHERE SYSDATE - HireDate > 365;
Output
EmployeeName NumberOfDays
Alfreds Futterkiste 200
Ana Trujillo Emparedados y helados 230
Antonio Moreno Taquería 120
Around the Horn 20
Above example describes use of subtraction operator in where clause. It displays employees whose joining date is within last year.