Szerkesztés

Megosztás a következőn keresztül:


Unary operators - Positive (Transact-SQL)

Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics Analytics Platform System (PDW) SQL analytics endpoint in Microsoft Fabric Warehouse in Microsoft Fabric

Returns the value of a numeric expression (a unary operator). Unary operators perform an operation on only one expression of any one of the data types of the numeric data type category.

Operator Meaning
+ (Unary positive) Numeric value is positive.
- (Unary negative) Numeric value is negative.
~ (Bitwise NOT) Returns the ones' complement of the number.

The + (positive) and - (negative) operators can be used on any expression of any one of the data types of the numeric data type category. The ~ (bitwise NOT) operator can be used only on expressions of any one of the data types of the integer data type category.

Transact-SQL syntax conventions

Syntax

+ numeric_expression

Arguments

numeric_expression

Any valid expression of any one of the data types in the numeric data type category, except the datetime and smalldatetime data types.

Return types

Returns the data type of numeric_expression.

Remarks

Although a unary plus can appear before any numeric expression, it performs no operation on the value returned from the expression. Specifically, it doesn't return the positive value of a negative expression. To return positive value of a negative expression, use the ABS function.

Examples

A. Set a variable to a positive value

The following example sets a variable to a positive value.

USE tempdb;
GO

DECLARE @MyNumber DECIMAL(10, 2);
SET @MyNumber = + 123.45;

SELECT @MyNumber AS PositiveValue;
GO

Here's the result set.

PositiveValue
--------------
123.45

B. Use the unary plus operator with a negative value

The following example shows using the unary plus with a negative expression and the ABS function on the same negative expression. The unary plus doesn't affect the expression, but the ABS() function returns the positive value of the expression.

USE tempdb;
GO

DECLARE @Num1 INT;
SET @Num1 = -5;

SELECT + @Num1 AS NegativeValue,
    ABS(@Num1) AS PositiveValue;
GO

Here's the result set.

NegativeValue  PositiveValue
-------------- --------------
-5             5