Friday, December 20, 2024

MySQL ABS() Function

 The ABS() function in MySQL returns the absolute value of a number, meaning it will convert negative numbers to positive, while positive numbers remain unchanged.

Syntax:

ABS(number);
  • number: The numeric expression whose absolute value you want to return.

Example:

  1. Using ABS() on a negative number:

    SELECT ABS(-45);
    

    Result: 45

  2. Using ABS() on a positive number:

    SELECT ABS(23);
    

    Result: 23

  3. Using ABS() on a column value:

    SELECT ABS(some_column) FROM some_table;
    

The function is useful when you need to ensure that the result is always positive, regardless of whether the original value is negative or positive.

No comments:

Post a Comment