SQL Server SIGN() Function
SIGN() returns 1 for a positive numeric expression, 0 for zero, and -1 for a negative expression. Its return type depends on the input type. See Microsoft’s SIGN() reference.
Syntax
SIGN ( numeric_expression )
numeric_expression must be an exact or approximate numeric expression; bit is not supported.
Return type
| Input type | Return type |
|---|---|
bigint |
bigint |
int, smallint, tinyint |
int |
money, smallmoney |
money |
numeric, decimal |
same numeric/decimal type |
| Other supported numeric types | float |
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
SIGN() returns a type determined by the input, with the value -1, 0, or 1. It does not always return an integer: for example, a bigint input returns bigint, and a decimal input returns decimal.