In SQL Server, the T-SQL SIGN()
function returns the sign of a number. In other words, it indicates whether or not the value is a positive number, a negative number, or zero.
You provide the number as an argument when calling the function.
The function returns the following result, depending on whether the argument is positive, negative, or zero.
- 1 if the number is positive
- -1 if the number is negative
- 0 if the number is zero
Syntax
The syntax goes like this:
SIGN ( numeric_expression )
Where numeric_expression is an expression of the exact numeric or approximate numeric data type category, except for the bit data type.
Example 1 – Basic Usage
Here’s a demonstration of passing a positive value to this function.
SELECT SIGN(5) Result;
Result:
+----------+ | Result | |----------| | 1 | +----------+
Example 2 – All Signs
Here’s an example using three different values — a negative value, a positive value, and zero.
SELECT SIGN(-5) Negative, SIGN(0) Zero, SIGN(5) Positive;
Result:
+------------+--------+------------+ | Negative | Zero | Positive | |------------+--------+------------| | -1 | 0 | 1 | +------------+--------+------------+
Example 3 – Expressions
You can also use expressions like this.
SELECT SIGN(50 - 60) Result;
Result:
+----------+ | Result | |----------| | -1 | +----------+
Example 4 – NULL Values
Passing a NULL value results in NULL
being returned.
SELECT SIGN(NULL) Result;
Result:
+----------+ | Result | |----------| | NULL | +----------+