In MySQL, the SIGN()
function returns the sign of a number. That is, it indicates whether or not the value is a positive number, a negative number, or zero.
You provide the value 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
This syntax goes like this:
SIGN(X)
Where X
is the value for which you’d like the sign returned.
Example 1 – Basic Usage
Here’s a demonstration of passing a positive value to this function.
SELECT SIGN(5);
Result:
+---------+ | SIGN(5) | +---------+ | 1 | +---------+
Example 2 – All Signs
Here’s an example using three different values — a negative value, a positive value, and zero.
SELECT SIGN(-5), SIGN(5), SIGN(0);
Result:
+----------+---------+---------+ | SIGN(-5) | SIGN(5) | SIGN(0) | +----------+---------+---------+ | -1 | 1 | 0 | +----------+---------+---------+
Example 3 – Expressions
You can also use expressions like this.
SELECT SIGN(50 - 60);
Result:
+---------------+ | SIGN(50 - 60) | +---------------+ | -1 | +---------------+
Example 4 – NULL Values
Passing a NULL value will result in NULL
being returned.
SELECT SIGN(NULL);
Result:
+------------+ | SIGN(NULL) | +------------+ | NULL | +------------+