Aan de slagGa gratis aan de slag

Extracting the sign and the absolute value

In some situations, you may need to use mathematical functions in your database development. After complex calculations, you may need to check the sign of an expression or its absolute value. The functions provided by SQL Server for these tasks are:

  • ABS(expression)
  • SIGN(expression)

In this exercise, you will work with the following variables:

DECLARE @number1 DECIMAL(18,2) = -5.4;
DECLARE @number2 DECIMAL(18,2) = 7.89;
DECLARE @number3 DECIMAL(18,2) = 13.2;
DECLARE @number4 DECIMAL(18,2) = 0.003;

The @result variable stores the result of the following calculation: @number1 * @number2 - @number3 - @number4.

You will calculate the absolute value and the sign of this expression.

Deze oefening maakt deel uit van de cursus

Functions for Manipulating Data in SQL Server

Cursus bekijken

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

DECLARE @number1 DECIMAL(18,2) = -5.4;
DECLARE @number2 DECIMAL(18,2) = 7.89;
DECLARE @number3 DECIMAL(18,2) = 13.2;
DECLARE @number4 DECIMAL(18,2) = 0.003;

DECLARE @result DECIMAL(18,2) = @number1 * @number2 - @number3 - @number4;
SELECT 
	@result AS result,
    -- Show the absolute value of the result
	___ AS abs_result;
Code bewerken en uitvoeren