Back to Library

SIGN() in SQL

Returns the sign of a number: -1 for negative numbers, 0 for zero, and 1 for positive numbers.

Syntax

SIGN(number)
Return type
INTEGER (-1, 0, or 1)

SIGN() Function Example

SELECT SIGN(-15.5); 
-- Output: -1

What is SIGN() in SQL?

The SIGN() function in SQL returns the sign of a given number, indicating whether the value is positive, negative, or zero. Specifically, it returns:

  • -1 if the number is negative

  • 0 if the number is zero

  • 1 if the number is positive

This function is useful for mathematical operations, financial calculations, and data analysis where distinguishing between positive and negative values is required. It is supported in SQL Server, MySQL, PostgreSQL, and Oracle, making it a reliable tool for handling numeric comparisons, trend analysis, and conditional logic in queries.

Parameters:

  • number: The numeric value whose sign is to be determined

Example Use Cases:

-- Determine direction of movement

SELECT SIGN(end_value - start_value) AS trend FROM stock_prices; 
-- Output:
date          start_value    end_value    trend
-----------   -----------    ---------    -----
2024-03-15    150.25        158.75       1     (upward movement)
2024-03-14    162.50        155.00       -1    (downward movement)
2024-03-13    162.50        162.50       0     (no change)
2024-03-12    145.75        152.25       1     (upward movement)

-- Check temperature changes 

SELECT SIGN(temperature_change) AS temp_direction FROM weather_data; 
-- Output:
reading_time        temperature_change    temp_direction
----------------    ------------------    --------------
2024-03-15 09:00    2.5                  1     (warming)
2024-03-15 06:00    -1.8                 -1    (cooling)
2024-03-15 03:00    0.0                  0     (stable)
2024-03-14 24:00    -3.2                 -1    (cooling)
2024-03-14 21:00    1.5                  1     (warming)

-- Analyze profit/loss 

SELECT SIGN(revenue - expenses) AS profit_indicator FROM financials;
-- Output:
month        revenue     expenses    profit_indicator
----------   --------    --------    ----------------
2024-03      75000.00    65000.00    1     (profit)
2024-02      62000.00    67000.00    -1    (loss)
2024-01      70000.00    70000.00    0     (break-even)
2023-12      85000.00    72000.00    1     (profit)
2023-11      58000.00    63000.00    -1    (loss)

Notes:

  • Behavior: Returns -1 for any negative number, 0 for zero, 1 for any positive number

  • Performance Considerations: Simple numeric operation, highly efficient

  • Version Info: Core mathematical function available in all major DBMS

  • Deprecated/Recommended Alternatives: None

Error Handling:

  • Error: Returns NULL if input is NULL

  • Recommendation: Use COALESCE if NULL handling needed

Supported Databases:

DBMS
Function / Syntax
Example
Behavior with NULL
MySQL

SIGN(X)

SIGN(-15.5)

Returns NULL

PostgreSQL

SIGN(number)

SIGN(-15.5)

Returns NULL

SQL Server

SIGN(numeric_expression)

SIGN(-15.5)

Returns NULL

SQLite

SIGN(X)

SIGN(-15.5)

Returns NULL

BigQuery

SIGN(X)

SIGN(-15.5)

Returns NULL

Snowflake

SIGN(number)

SIGN(-15.5)

Returns NULL

Athena

SIGN(number)

SIGN(-15.5)

Returns NULL

Related Functions:

SIGN()

Returns the sign of a number: -1 for negative numbers, 0 for zero, and 1 for positive numbers.

Returns the sign of a number: -1 for negative numbers, 0 for zero, and 1 for positive numbers.

Returns the sign of a number: -1 for negative numbers, 0 for zero, and 1 for positive numbers.

Returns the sign of a number: -1 for negative numbers, 0 for zero, and 1 for positive numbers.

RAND()/RANDOM()

Generates a random number. Without parameters, returns a floating-point number between 0 (inclusive) and 1 (exclusive). Some DBMS allow seeding for reproducible results.

Generates a random number. Without parameters, returns a floating-point number between 0 (inclusive) and 1 (exclusive). Some DBMS allow seeding for reproducible results.

Generates a random number. Without parameters, returns a floating-point number between 0 (inclusive) and 1 (exclusive). Some DBMS allow seeding for reproducible results.

Generates a random number. Without parameters, returns a floating-point number between 0 (inclusive) and 1 (exclusive). Some DBMS allow seeding for reproducible results.

MOD()

Returns the remainder after dividing one number by another. Also available as % operator in many databases.

Returns the remainder after dividing one number by another. Also available as % operator in many databases.

Returns the remainder after dividing one number by another. Also available as % operator in many databases.

Returns the remainder after dividing one number by another. Also available as % operator in many databases.

SQRT()

Returns the square root of a non-negative number. The square root is the value that, when multiplied by itself, gives the number.

Returns the square root of a non-negative number. The square root is the value that, when multiplied by itself, gives the number.

Returns the square root of a non-negative number. The square root is the value that, when multiplied by itself, gives the number.

Returns the square root of a non-negative number. The square root is the value that, when multiplied by itself, gives the number.

POWER()

Returns the result of raising a base number to a specified power (exponent). Also written as POW() in some databases.

Returns the result of raising a base number to a specified power (exponent). Also written as POW() in some databases.

Returns the result of raising a base number to a specified power (exponent). Also written as POW() in some databases.

Returns the result of raising a base number to a specified power (exponent). Also written as POW() in some databases.

One place for all your queries,
directly on your SQL editor