SQL Server Articles

Submit Article
Home » Articles » SQL Server » Operator RSS Feeds

Examples of - ( Subtract ) Operator

Posted By: Sarita Patel     Category: SQL Server     Views: 13426

This article explains -( Subtract ) operator with different examples.

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.



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

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;

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.

Didn't find what you were looking for? Find more on Examples of - ( Subtract ) Operator Or get search suggestion and latest updates.

Sarita Patel
Sarita Patel author of Examples of - ( Subtract ) Operator is from United States.
View All Articles

Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

No Comment Found, Be the First to post comment!