Skip to content

Latest commit

 

History

History
109 lines (82 loc) · 4.28 KB

subtract-transact-sql.md

File metadata and controls

109 lines (82 loc) · 4.28 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic ms.custom f1_keywords helpviewer_keywords dev_langs monikerRange
- (Subtraction) (Transact-SQL)
- (Subtraction) (Transact-SQL)
rwestMSFT
randolphwest
03/15/2017
sql
t-sql
reference
ignite-2024
subtract
-
-_TSQL
- (subtract)
subtract operator (-)
minus operator (-)
subtracting numbers
TSQL
>=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric

- (Subtraction) (Transact-SQL)

[!INCLUDE sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw-fabricsqldb]

Subtracts two numbers (an arithmetic subtraction operator). Can also subtract a number, in days, from a date.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

expression - expression  

Arguments

expression
Is any valid expression of any one of the data types of the numeric data type category, except the bit data type. Cannot be used with date, time, datetime2, or datetimeoffset data types.

Result Types

Returns the data type of the argument with the higher precedence. For more information, see Data Type Precedence (Transact-SQL).

Examples

A. Using subtraction in a SELECT statement

The following example calculates the difference in tax rate between the state or province with the highest tax rate and the state or province with the lowest tax rate.

Applies to: [!INCLUDEssNoVersion] and [!INCLUDEssSDS].

-- Uses AdventureWorks  
  
SELECT MAX(TaxRate) - MIN(TaxRate) AS 'Tax Rate Difference'  
FROM Sales.SalesTaxRate  
WHERE StateProvinceID IS NOT NULL;  
GO  

You can change the order of execution by using parentheses. Calculations inside parentheses are evaluated first. If parentheses are nested, the most deeply nested calculation has precedence.

B. Using date subtraction

The following example subtracts a number of days from a datetime date.

Applies to: [!INCLUDEssNoVersion] and [!INCLUDEssSDS].

-- Uses the AdventureWorks sample database
DECLARE @altstartdate DATETIME;  
SET @altstartdate = CONVERT(DATETIME, 'January 10, 1900 3:00 AM', 101);  
SELECT @altstartdate - 1.5 AS 'Subtract Date';  

Here is the result set:

Subtract Date  
-----------------------  
1900-01-08 15:00:00.000  

(1 row(s) affected)

Examples: [!INCLUDEssazuresynapse-md] and [!INCLUDEssPDW]

C: Using subtraction in a SELECT statement

The following example calculates the difference in a base rate between the employee with the highest base rate and the employee with the lowest tax rate, from the dimEmployee table.

-- Uses AdventureWorks  
  
SELECT MAX(BaseRate) - MIN(BaseRate) AS BaseRateDifference  
FROM DimEmployee;  

See Also

-= (Subtraction Assignment) (Transact-SQL)
Compound Operators (Transact-SQL)
Arithmetic Operators (Transact-SQL)
- (Negative) (Transact-SQL)
Data Types (Transact-SQL)
Expressions (Transact-SQL)
Built-in Functions (Transact-SQL)
SELECT (Transact-SQL)