SQL ABS Function

The SQL ABS function is a Mathematical Function that will return the absolute positive value of the specified expression. The syntax of the SQL Server ABS Function to find absolute value is:

SELECT ABS (Numeric_Expression)
FROM [Source]

SQL ABS Function Example 1

The SQL Server ABS Function returns the absolute positive value of any numeric value. The following Mathematical Function query will show multiple ways to use the ABS function.

-- Sql Server Absolute Value
DECLARE @i int
SET @i = -25.8

SELECT ABS(@i) AS [Absolute Value]

-- Calculating ABS directly
SELECT ABS(-27) AS [Absolute Value]

-- Calculating ABS directly
SELECT ABS(2 + 55 - 77) AS [Absolute Value]
SQL ABS FUNCTION 1

We used the ABS function to find the absolute value of the variable @i. It means ABS(-25.8) and assigned a new name ‘Absolute Value’ using the ALIAS Column.

SELECT ABS(@i) AS [Absolute Value]

In the next SQL Server statement, We used the absolute directly on the int value.

SELECT ABS(-27) AS [Absolute Value]

We used the ABS Function directly on the multiple values.

SELECT ABS(2 + 55 - 77) AS [Absolute Value]

It means ABS(2 + 55 – 77) => ABS (-20) = 20

ABS Function Example 2

The ABS Function also allows you to find the absolute values of a column values. Here, we are going to find the absolute values for all the records present in [Service Grade] using this ABS Function.

For this SQL Server ABS function demo, we use the Math Table data

-- SQL Absolute Value
SELECT [EnglishProductName]
      ,[Color]
      ,[StandardCost]
      ,[SalesAmount]
      ,[Service Grade]
      ,ABS([Service Grade]) AS [Absolute Value] 
  FROM [Mathemetical Functions]
SQL ABS FUNCTION 2