The SIGN() function is used to determine the sign of a given number.
Definition and Usage
- Returns 1 if the number is positive.
- Returns 0 if the number is zero.
- Returns -1 if the number is negative.
Syntax
SIGN(number)
Parameter Values
| Parameter | Description |
|---|---|
number |
The numeric value to evaluate. |
Technical Details
- Introduced in: MySQL 4.0
- Return Value:
1,0, or-1
Examples
Example 1: Positive number
SELECT SIGN(255.5);
Output:
1
Example 2: Negative number
SELECT SIGN(-100);
Output:
-1
Example 3: Zero
SELECT SIGN(0);
Output:
0
Example 4: Small negative decimal
SELECT SIGN(-0.0001);
Output:
-1
Example 5: Use in a column
Determine the sign for each value in a column:
SELECT Price, SIGN(Price) AS PriceSign
FROM Products;
Output (example):
| Price | PriceSign |
|---|---|
| 100.00 | 1 |
| -50.00 | -1 |
| 0.00 | 0 |
Use Cases
- Evaluate whether numbers are positive, negative, or zero.
- Useful in mathematical computations and conditional logic in queries.
Related Functions
ABS(): Returns the absolute value of a number.ROUND(): Rounds a number to the nearest integer.