SQL Server SIGN() Function
The SIGN()
function in SQL Server returns the sign of a given number, i.e., 1 for positive numbers, -1 for negative numbers, and 0 for zero.
Syntax
SIGN ( numeric_expression )
The numeric_expression
is the number expression for which the sign needs to be calculated.
Usage
The SIGN()
function can be used in situations where it is necessary to check whether a number is positive, negative, or zero. It can also be used to check the sign of a given number before performing mathematical calculations.
Examples
Example 1
The following example shows the sign of -50, 0, and 50.
SELECT SIGN(-50) AS Negative,
SIGN(0) AS Zero,
SIGN(50) AS Positive;
Result:
Negative | Zero | Positive |
---|---|---|
-1 | 0 | 1 |
Example 2
The following example checks the sign of a column in a table.
CREATE TABLE Numbers (value INT);
INSERT INTO Numbers (value)
VALUES (-10), (0), (10);
SELECT value, SIGN(value) AS Sign
FROM Numbers;
Result:
value | Sign |
---|---|
-10 | -1 |
0 | 0 |
10 | 1 |
Conclusion
The SIGN()
function is useful in situations where it is necessary to check whether a number is positive, negative, or zero. It returns an integer value, where -1 represents a negative number, 0 represents zero, and 1 represents a positive number.