Export (0) Print
Expand All
Expand Minimize
12 out of 16 rated this helpful - Rate this topic

+ (Add) (Transact-SQL)

Adds two numbers. This addition arithmetic operator can also add a number, in days, to a datetime.

Topic link iconTransact-SQL Syntax Conventions

expression +expression
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 or time data types.

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

A. 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.

USE AdventureWorks;
GO
SELECT c.FirstName, c.LastName, VacationHours, SickLeaveHours, 
    VacationHours + SickLeaveHours AS 'Total Hours Away'
FROM HumanResources.Employee AS e
    JOIN Person.Contact AS c ON e.ContactID = c.ContactID
ORDER BY 'Total Hours Away' ASC;
GO

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

The following example adds a number of days to a datetime date.

USE master;
GO
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'

Here is the result set.

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 SQL Server Database Engine returns an error.

DECLARE @addvalue int
SET @addvalue = 15
SELECT '125127' + @addvalue

Here is the result set.

-----------------------

125142

(1 row(s) affected)

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.