+(加法)(Transact-SQL)+ (Addition) (Transact-SQL)

适用对象: 是SQL Server 是Azure SQL 数据库 是Azure Synapse Analytics (SQL DW) 是并行数据仓库 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure Synapse Analytics (SQL DW) yesParallel Data Warehouse

两个数相加。Adds two numbers. 这个加法算术运算符也可以将一个以天为单位的数字加到日期中。This addition arithmetic operator can also add a number, in days, to a date.

主题链接图标 TRANSACT-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

expression + expression  

参数Arguments

expressionexpression
数值类别中任意数据类型(bit 数据类型除外)的任何有效表达式Is any valid expression of any one of the data types in the numeric category except the bit data type. 不能与 date、time、datetime2 或 datetimeoffset 数据类型一起使用。Cannot be used with date, time, datetime2, or datetimeoffset data types.

结果类型Result Types

返回优先级较高的参数的数据类型。Returns the data type of the argument with the higher precedence. 有关详细信息,请参阅数据类型优先级 (Transact-SQL)For more information, see Data Type Precedence (Transact-SQL).

示例Examples

A.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.B. 使用加法运算符将天数添加到日期和时间值中Using the addition operator to add days to date and time values

此示例将若干天添加到 datetime 日期上。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';  

下面是结果集: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.C. 将字符和整数数据类型相加Adding character and integer data types

以下示例通过将字符数据类型转换为 int,将 int 数据类型值与字符值相加。如果 char 字符串中具有无效字符,则 Transact-SQLTransact-SQL 返回一个错误。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 Transact-SQLTransact-SQL returns an error.

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

下面是结果集:Here is the result set.

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

示例:Azure Synapse Analytics (SQL DW)Azure Synapse Analytics (SQL DW)并行数据仓库Parallel Data WarehouseExamples: Azure Synapse Analytics (SQL DW)Azure Synapse Analytics (SQL DW) and 并行数据仓库Parallel Data Warehouse

D:使用加法运算符计算每个雇员离岗的总小时数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

运算符 (Transact-SQL) Operators (Transact-SQL)
复合运算符 (Transact-SQL) Compound Operators (Transact-SQL)
(加法赋值)(Transact-SQL) += (Addition Assignment) (Transact-SQL)
CAST 和 CONVERT (Transact-SQL) CAST and CONVERT (Transact-SQL)
数据类型转换(数据库引擎) Data Type Conversion (Database Engine)
数据类型 (Transact-SQL) Data Types (Transact-SQL)
内置函数 (Transact-SQL) Built-in Functions (Transact-SQL)
SELECT (Transact-SQL)SELECT (Transact-SQL)