How the SIGN() function works in Mariadb?

The SIGN() function in MariaDB is a simple but useful function that returns the sign of a given numeric value.

Posted on

The SIGN() function in MariaDB is a simple but useful function that returns the sign of a given numeric value. It determines whether the input value is positive, negative, or zero, and returns a corresponding integer value of 1, -1, or 0, respectively.

Syntax

The syntax for the MariaDB SIGN() function is as follows:

SIGN(X)
  • X: The numeric value for which the sign is to be determined.

The function returns an integer value:

  • If X is positive, it returns 1.
  • If X is negative, it returns -1.
  • If X is zero, it returns 0.

Example: Determining the sign of various numeric values

In this example, we’ll use the SIGN() function to determine the sign of different numeric values.

SELECT SIGN(42), SIGN(-3.14), SIGN(0);

The following is the output:

+----------+-------------+---------+
| SIGN(42) | SIGN(-3.14) | SIGN(0) |
+----------+-------------+---------+
|        1 |          -1 |       0 |
+----------+-------------+---------+

The SIGN() function correctly identifies the sign of the positive value 42 as 1, the negative value -3.14 as -1, and the zero value 0 as 0.

The following are some functions related to the MariaDB SIGN() function:

  • MariaDB ABS() function is used to return the absolute value of a numeric value.
  • MariaDB CEIL() function is used to round a numeric value up to the nearest integer.
  • MariaDB FLOOR() function is used to round a numeric value down to the nearest integer.
  • MariaDB ROUND() function is used to round a numeric value to a specified number of decimal places.
  • MariaDB TRUNCATE() function is used to truncate a numeric value to a specified number of decimal places.

Conclusion

The SIGN() function in MariaDB is a simple but powerful tool that can be used in various scenarios where you need to determine the sign of a numeric value. It can be particularly useful in conditional logic, data validation, and mathematical operations. While the SIGN() function itself is straightforward, it can be combined with other functions and conditional statements to perform more complex operations and calculations based on the sign of a value.