In MariaDB, SIGN() è una funzione incorporata che restituisce il segno del suo argomento come -1 , 0 o 1 , a seconda che l'argomento sia negativo, zero o positivo.
Sintassi
La sintassi è questa:
SIGN(X)
Dove X è il valore per il quale restituire il segno.
Esempio 1
Ecco un esempio da dimostrare:
SELECT SIGN(9); Risultato:
+---------+ | SIGN(9) | +---------+ | 1 | +---------+
In questo esempio, il valore è positivo, quindi 1 viene restituito.
Esempio 2
Ecco alcuni altri valori per dimostrare i possibili output:
SELECT
SIGN(8),
SIGN(-8),
SIGN(0); Risultato:
+---------+----------+---------+ | SIGN(8) | SIGN(-8) | SIGN(0) | +---------+----------+---------+ | 1 | -1 | 0 | +---------+----------+---------+
Argomenti non numerici
Ecco un esempio di cosa succede quando forniamo argomenti non numerici:
SELECT SIGN('Nine'); Risultato:
+--------------+
| SIGN('Nine') |
+--------------+
| 0 |
+--------------+
1 row in set, 1 warning (0.000 sec) Vediamo l'avviso:
SHOW WARNINGS; Risultato:
+---------+------+------------------------------------------+ | Level | Code | Message | +---------+------+------------------------------------------+ | Warning | 1292 | Truncated incorrect DOUBLE value: 'Nine' | +---------+------+------------------------------------------+
Argomento nullo
SIGN() restituisce null se il suo argomento è null :
SELECT SIGN(null); Risultato:
+------------+ | SIGN(null) | +------------+ | NULL | +------------+
Conteggio parametri errato
Chiamando SIGN() con il numero errato di argomenti o senza argomenti genera un errore:
SELECT SIGN(); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'SIGN'
E:
SELECT SIGN(10, 2); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'SIGN'