Mathematical functions

Functions for Manipulating Data in SQL Server

Ana Voicu

Data Engineer

ABS(numeric_expression)

  • Returns the absolute value of an expression.
  • Is the non-negative value of the expression.
SELECT 
    ABS(-50.4 *3) AS negative,
    ABS(0.0) AS zero,
    ABS(73.2 + 15 + 8.4) AS positive;
| negative | zero | positive |
|----------|------|----------|
| 151.2    | 0    | 96.6     |
Functions for Manipulating Data in SQL Server

SIGN(numeric_expression)

  • Returns the sign of an expression, as an integer:
    • -1 (negative numbers)
    • 0
    • +1 (positive numbers)
SELECT 
    SIGN(-50.4 *3) AS negative,
    SIGN(0.0) AS zero,
    SIGN(73.2 + 15 + 8.4) AS positive;
| negative | zero | positive |
|----------|------|----------|
| -1.0     | 0    | 1.0      |
Functions for Manipulating Data in SQL Server

Rounding functions

  • CEILING(numeric_expression)
    • Returns the smallest integer greater than or equal to the expression.
  • FLOOR(numeric_expression)
    • Returns the largest integer less than or equal to the expression.
  • ROUND(numeric_expression, length)
    • Returns a numeric value, rounded to the specified length.
Functions for Manipulating Data in SQL Server

Rounding functions example

SELECT 
    CEILING(-50.49) AS ceiling_neg,
    CEILING(73.71) AS ceiling_pos;

| ceiling_neg | ceiling_pos | 
|-------------|-------------|
| -50         | 74          | 

Functions for Manipulating Data in SQL Server

Rounding functions example

SELECT 
    CEILING(-50.49) AS ceiling_neg,    
    FLOOR(-50.49) AS floor_neg,
    CEILING(73.71) AS ceiling_pos,
    FLOOR(73.71) AS floor_pos;

| ceiling_neg | floor_neg | ceiling_pos | floor_pos | 
|-------------|-----------|-------------|-----------|
| -50         | -51       | 74          | 73        | 

Functions for Manipulating Data in SQL Server

Rounding functions example

SELECT 
    CEILING(-50.49) AS ceiling_neg,    
    FLOOR(-50.49) AS floor_neg,
    CEILING(73.71) AS ceiling_pos,
    FLOOR(73.71) AS floor_pos,
    ROUND(-50.493, 1)AS round_neg,
    ROUND(73.715, 2) AS round_pos;
| ceiling_neg | floor_neg | ceiling_pos | floor_pos | round_neg | round_pos |
|-------------|-----------|-------------|-----------|-----------|-----------|
| -50         | -51       | 74          | 73        | -50.500   | 73.720    |
Functions for Manipulating Data in SQL Server

Exponential functions

  • POWER(numeric_expression, power)
    • Returns the expression raised to the specified power.
  • SQUARE(numeric_expression)
    • Returns the square of the expression.
  • SQRT(numeric_expression)
    • Returns the square root of the expression.
  • Keep in mind: the type of the expression is float or can be implicitly converted to float.
Functions for Manipulating Data in SQL Server

POWER() example

SELECT   
POWER(2, 10)  AS pos_num,
POWER(-2, 10) AS neg_num_even_pow,
POWER(-2, 11) AS neg_num_odd_power,
POWER(2.5, 2) AS float_num,
POWER(2, 2.72) AS float_pow;
| pos_num | neg_num_even_pow | neg_num_odd_pow | float_num | float_pow |
|---------|------------------|-----------------|-----------|-----------|
| 1024    | 1024             | -2048           | 6.3       | 6         |
Functions for Manipulating Data in SQL Server

SQUARE() example

SELECT   
SQUARE(2)  AS pos_num,
SQUARE(-2) AS neg_num,
SQUARE(2.5) AS float_num;
| pos_num | neg_num | float_num |
|---------|---------|-----------|
| 4       | 4       | 6.25      |
Functions for Manipulating Data in SQL Server

SQRT() example

SELECT   
SQRT(2)  AS int_num,
SQRT(2.76) AS float_num;
| int_num         | float_num        |
|-----------------|------------------|
| 1.4142135623731 | 1.66132477258361 |
Functions for Manipulating Data in SQL Server

Let's practice!

Functions for Manipulating Data in SQL Server

Preparing Video For Download...