sqlcmd.net validated sql reference
beginner math MySQL MariaDB SQL Server PostgreSQL SQLite

Determine The Sign Of A Number With SIGN

Use `SIGN` to reduce any number to -1, 0, or 1 based on whether it is negative, zero, or positive.

Docker-validated Not currently validation-green

Classify each sensor reading as rising, stable, or falling

Sensor A has a positive delta (15), so SIGN returns 1. Sensor B has no change (0), so SIGN returns 0. Sensor C has a negative delta (-8), so SIGN returns -1. The magnitude of the delta is discarded — only the direction is preserved.

Rows loaded before the example query runs.
Setup
CREATE TABLE readings (sensor VARCHAR(10), delta INT);

INSERT INTO
  readings
VALUES
  ('A', 15),
  ('B', 0),
  ('C', -8);
Shared across supported engines.
SQL
SELECT
  sensor,
  delta,
  SIGN (delta) AS direction
FROM
  readings
ORDER BY
  sensor;
Returned rows for the shared example.
sensordeltadirection
A151
B00
C-8-1

Identical syntax and result across all engines.

Where this command helps.

  • converting a numeric delta into a direction indicator (-1, 0, or 1)
  • grouping rows by whether a value increased, stayed the same, or decreased

What the command is doing.

SIGN(n) returns -1 if n is negative, 0 if n is zero, and 1 if n is positive. It is useful for normalizing a delta or difference to a direction, for sorting by direction without caring about magnitude, and for converting a comparison result into a number that can be multiplied or summed. All major engines support SIGN with the same syntax.