ABS (Transact-SQL)

Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics Analytics Platform System (PDW)

A mathematical function that returns the absolute (positive) value of the specified numeric expression. (ABS changes negative values to positive values. ABS has no effect on zero or positive values.)

Transact-SQL syntax conventions

Syntax

ABS ( numeric_expression )  

Note

To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.

Arguments

numeric_expression
An expression of the exact numeric or approximate numeric data type category.

Return types

Returns the same type as numeric_expression.

Examples

This example shows the results of using the ABS function on three different numbers.

SELECT ABS(-1.0), ABS(0.0), ABS(1.0);  

Here is the result set.

---- ---- ----  
1.0  .0   1.0  

The ABS function can produce an overflow error when the absolute value of a number exceeds the largest number that the specified data type can represent. For example, the int data type has a value range from -2,147,483,648 to 2,147,483,647. Computing the absolute value for the signed integer -2,147,483,648 will cause an overflow error because its absolute value exceeds the positive range limit for the int data type.

DECLARE @i INT;  
SET @i = -2147483648;  
SELECT ABS(@i);  
GO  

Returns this error message:

"Msg 8115, Level 16, State 2, Line 3"

"Arithmetic overflow error converting expression to data type int."

See also

CAST and CONVERT (Transact-SQL)
Data Types (Transact-SQL)
Mathematical Functions (Transact-SQL)
Built-in Functions (Transact-SQL)