SQL SIGN function
Returns -1, 0 or 1 depending on the input's sign. Applied to a difference, it says behind, level or ahead.
- Categoría
- Math
- Returns
-1, 0 or 1, in the input's type on SQL Server and PostgreSQL- In this category
- 9 of 82
Sintaxis
SIGN(number)
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Sí | Supported under this name. |
| PostgreSQL | Sí | Supported under this name. |
| MySQL | Sí | Supported under this name. |
| SQLite | Sí | Supported under this name. |
| Oracle | Sí | Supported under this name. |
Ejemplos
Any engine
SELECT SIGN(-19.5) AS direction; -- -1
Any engine
SELECT region, SIGN(actual - target) AS direction, COUNT(*) AS reps FROM quotas GROUP BY region, SIGN(actual - target);
Conviene saberlo
- The value is always -1, 0 or 1, but the type can follow the input: SIGN(-19.5) is -1.0 (decimal) on SQL Server and a numeric on PostgreSQL, and an integer on MySQL and SQLite.
- SIGN of NULL is NULL, not 0.
- SIGN is a core function in SQLite, available even in builds without the math functions.
- SUM(SIGN(actual - target)) is rows ahead minus rows behind. To count rows ahead, use SUM(CASE WHEN actual > target THEN 1 ELSE 0 END).
Questions about SIGN
What does SIGN(0) return?
0. SIGN returns -1, 0 or 1, and only returns NULL when the input is NULL.
How do SIGN and ABS relate?
ABS keeps the magnitude and SIGN keeps the direction, and multiplying them gives the original number back: SIGN(x) * ABS(x) = x.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.