Any suggestions? Export (0) Print
Expand All

ABS (Transact-SQL)

 

THIS TOPIC APPLIES TO:yesSQL Server (starting with 2008) yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

A mathematical function that returns the absolute (positive) value of the specified numeric expression.

Topic link icon Transact-SQL Syntax Conventions


ABS ( numeric_expression )
-- Azure SQL Data Warehouse and Parallel Data Warehouse
ABS (numeric_expression )

numeric_expression

Is an expression of the exact numeric or approximate numeric data type category.

Returns the same type as numeric_expression.

The following 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 is greater than the largest number that can be represented by the specified data type. For example, the int data type can hold only values that range from -2,147,483,648 to 2,147,483,647. Computing the absolute value for the signed integer -2,147,483,648 causes an overflow error because its absolute value is greater than the positive range for the int data type.

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

Here is the error message:

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

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

The following 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.0  1.0

Community Additions

ADD
Show:
© 2016 Microsoft