Skip to main content

ABS

The ABS function in SQL is used to return the absolute value of a number, i.e., the numeric value without its sign. The function takes a single argument which must be a number (integer, float, etc.) and returns the absolute, non-negative equivalent.

The general syntax for the ABS function is as follows:

ABS(expression)

In the syntax above, the expression is required and can either be a literal number, a column name, the result of another function, or any valid SQL expression that resolves to a numeric value.

Examples

Consider a database table Orders:

OrderIDProductQuantity
1Apple-5
2Banana10
3Cherry-15

If you want to get the absolute value of the 'Quantity' column, you could use the ABSfunction like this:

SELECT OrderID, Product, ABS(Quantity) as 'Absolute Quantity'
FROM Orders;

The output will be:

OrderIDProductAbsolute Quantity
1Apple5
2Banana10
3Cherry15

As you can see, the negative values in the 'Quantity' column have been converted to positive values by the ABS function.