MySQL ABS Function

MySQL ABS function is one of the Numeric Functions, which is beneficial to return the absolute positive value of the specified expression. In this article, we show how to find Absolute Values in MySQL using Command Prompt and Workbench with example.

MySQL ABS Syntax

The basic syntax of the ABS in MySQL is as shown below:

SELECT ABS (Numeric_Expression)
FROM Source

To demonstrate this Numeric function, we are going to use the below shown data

MySQL ABS Function 1

MySQL ABS Function Example 1

The ABS Function returns the positive value of any numeric value. The following MySQL query shows multiple ways to use the ABS function.

-- MySQL ABS Function Example

-- Numeric Value
SELECT ABS(-52) AS `Absolute Value`;

-- Absolute Value of Numeric Expression
SELECT ABS(10 + 35 - 102) AS `Absolute Value`;

-- Absolute Value of string value
SELECT ABS('-10.02') AS `Absolute Value`;

-- Absolute Value of String
SELECT ABS('MySQL') AS `Absolute Value`;

In the below statement, We used the ABS function to find the absolute value of different values. Here, we assigned a new name to the result as ‘Absolute Value’ using the ALIAS Column in MySQL.

MySQL ABS Function 2

MySQL ABS Example 2

The MySQL ABS Numeric Function also allows you to find the absolute value of the data in a column. In this example, we are going to find the absolute values for all the records present in the ServiceGrade column.

-- MySQL Absolute Values Example
USE mysqltutorial;
SELECT Product, Color,
		StandardCost, Sales, TaxAmt, 
        ABS(ServiceGrade) AS `Absolute Value`
FROM `numeric functions`;

MySQL ABS Function 3