MySQL SIGN() Function
The SIGN()
function in MySQL determines the sign of a number and returns a value indicating whether the number is positive, negative, or zero.
SIGN(): Definition and Usage
This function is a simple yet useful mathematical function. It's often used in conditional logic or calculations where you need to know the sign of a value without needing the actual magnitude.
Return Values
The SIGN()
function returns:
1
if the number is positive0
if the number is zero-1
if the number is negative
Syntax
Syntax
SIGN(number)
Parameter Values
Parameter | Description |
---|---|
number |
The number for which you want to determine the sign. This is required. |
Examples
Determining the Sign of a Positive Number
This example shows how SIGN()
returns 1 for a positive number.
Syntax
SELECT SIGN(255.5);
Output
1
Determining the Sign of a Negative Number
Here, SIGN()
correctly identifies a negative number and returns -1.
Syntax
SELECT SIGN(-12);
Output
-1