Sign Function In Sql in SQL
The SIGN function in SQL is a mathematical function that returns the sign of a numeric expression—i.e., it tells you whether a number is positive, negative, or zero.
? Syntax
SIGN(numeric_expression)? Return Values
Value of numeric_expression | SIGN Returns |
|---|---|
Positive number (> 0) | 1 |
Zero (= 0) | 0 |
Negative number (< 0) | -1 |
? Example Usage
SELECT SIGN(100) AS Result1, -- Returns 1 SIGN(0) AS Result2, -- Returns 0 SIGN(-45.67) AS Result3; -- Returns -1? Use Cases
Determining the direction of a value (e.g., gain vs loss, trend up/down).
Simplifying conditional logic in financial or mathematical queries.
Assisting in ranking or sorting based on numeric direction.
? Example in a Table Query
Suppose you have a Transactions table with a BalanceChange column:
SELECT TransactionID, BalanceChange, SIGN(BalanceChange) AS ChangeDirectionFROM Transactions;This will help you quickly identify:
1: credit/gain-1: debit/loss0: no change
Let me know if you'd like a stored procedure or case-based logic that leverages SIGN for more advanced reporting or analysis.