SIGN Function - SQL
Overview
The SIGN function in SQL returns the sign of a numeric expression. It indicates whether the number is positive, negative, or zero.
Example:
SELECT
SIGN(-10)
Syntax:
SELECT
SIGN(numeric_expression)
numeric_expression
is the number for which you want to determine the sign. The function returns 1
if the number is positive, -1
if it is negative, and 0
if it is zero.
Sample Data:
first_name | vacation_days |
---|---|
Frank | 5 |
Jane | 2 |
Ashley | 3 |
Glenn | -3 |
Kelly | 2 |
Richard | -7 |
George | 2 |
Kyle | 1 |
James | -2 |
Gustavo | -10 |
Example: Hard-coded value
In this example, we are returning the value of the sign for -10, since it is a negative number the final output is -1.
Example: Query without the WHERE Statement
In this example, we are returning the value of the signs for each row in the vacation_days column.
Example: Query with the WHERE Statement
In this example, we are returning the value of the signs for each row in the vacation_days column where the first_name column equals ‘Kelly.’