Skip to content

Latest commit

 

History

History
128 lines (100 loc) · 4.85 KB

add-transact-sql.md

File metadata and controls

128 lines (100 loc) · 4.85 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic ms.custom f1_keywords helpviewer_keywords dev_langs monikerRange
+ (Addition) (Transact-SQL)
+ (Addition) (Transact-SQL)
rwestMSFT
randolphwest
03/16/2017
sql
t-sql
reference
ignite-2024
add
+ (Add)
+_TSQL
addition (+)
adding numbers
+ (add)
plus sign (+)
add operator (+)
TSQL
>=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric

+ (Addition) (Transact-SQL)

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

Adds two numbers. This addition arithmetic operator can also add a number, in days, to 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 in the numeric 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 the addition operator to calculate the total number of hours away from work for each employee.

This example finds the total number of hours away from work for each employee by adding the number of hours taken for vacation and the number of hours taken as sick leave.

-- Uses AdventureWorks  
  
SELECT p.FirstName, p.LastName, VacationHours, SickLeaveHours,   
    VacationHours + SickLeaveHours AS 'Total Hours Away'  
FROM HumanResources.Employee AS e  
    JOIN Person.Person AS p ON e.BusinessEntityID = p.BusinessEntityID  
ORDER BY 'Total Hours Away' ASC;  
GO  

B. Using the addition operator to add days to date and time values

This example adds a number of days to a datetime date.

SET NOCOUNT ON  
DECLARE @startdate DATETIME, @adddays INT;  
SET @startdate = 'January 10, 1900 12:00 AM';  
SET @adddays = 5;  
SET NOCOUNT OFF;  
SELECT @startdate + 1.25 AS 'Start Date',   
   @startdate + @adddays AS 'Add Date';  

[!INCLUDEssResult]

Start Date                  Add Date
--------------------------- ---------------------------
1900-01-11 06:00:00.000     1900-01-15 00:00:00.000
 
(1 row(s) affected)

C. Adding character and integer data types

The following example adds an int data type value and a character value by converting the character data type to int. If a character that is not valid exists in the char string, the [!INCLUDEtsql] returns an error.

DECLARE @addvalue INT;  
SET @addvalue = 15;  
SELECT '125127' + @addvalue;  

[!INCLUDEssResult]

-----------------------
125142
 
(1 row(s) affected)

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

D: Using the addition operator to calculate the total number of hours away from work for each employee

The following example finds the total number of hours away from work for each employee by adding the number of hours taken for vacation and the number of hours taken as sick leave and sorts the results in ascending order.

-- Uses AdventureWorks  
  
SELECT FirstName, LastName, VacationHours, SickLeaveHours,   
    VacationHours + SickLeaveHours AS TotalHoursAway  
FROM DimEmployee  
ORDER BY TotalHoursAway ASC;  

See Also

Operators (Transact-SQL)
Compound Operators (Transact-SQL)
+= (Addition Assignment) (Transact-SQL)
CAST and CONVERT (Transact-SQL)
Data Type Conversion (Database Engine)
Data Types (Transact-SQL)
Built-in Functions (Transact-SQL)
SELECT (Transact-SQL)