Subtracts two numbers. This subtraction arithmetic operator can also subtract a number, in days, from a date.
expression - expression
Is any valid Microsoft® SQL Server™ expression of any of the data types of the numeric data type category except the bit data type.
Returns the data type of the argument with the higher precedence. For more information, see Data Type Precedence.
A. Use subtraction in a SELECT statement
This example returns the amount of the year-to-date revenues retained by the company for each book title.
USE pubs GO SELECT title, ( (price * ytd_sales) * CAST( ( (100 - royalty) / 100.0 ) AS MONEY) ) AS IncomeAfterRoyalty FROM titles WHERE royalty <> 0 ORDER BY title_id ASC GO
Parentheses can be used to change the order of execution. Calculations inside parentheses are evaluated first. If parentheses are nested, the most deeply nested calculation has precedence. For example, the result and meaning of the preceding query can be changed if you use parentheses to force the evaluation of subtraction before multiplication, which in this case would yield a meaningless number.
B. Use date subtraction
This example subtracts a number of days from a datetime date.
USE pubs GO DECLARE @altstartdate datetime SET @altstartdate = '1/10/1900 3:00 AM' SELECT @altstartdate - 1.5 AS 'Subtract Date'
Here is the result set:
Subtract Date -------------------- Jan 8 1900 3:00PM (1 row(s) affected)