Chapter 11-2 SQL Functions - Numeric
Chapter 11-2 SQL Functions - Numeric
Chapter 11
Numeric Functions
• ROUND
• DECIMAL
• TRUNC
• MOD
• CEILING
• FLOOR
• ABS
2
ROUND & DECIMAL Functions
• The ROUND function rounds a numeric value to a specified
number of decimals
3
ROUND Function
• Used with both numbers and dates
• Mainly used to round numbers to a specified number of decimal places
• Can be used to round numbers to the left of the decimal point
• If the number of decimal places is not specified or is zero, the number will round
to no decimal places
4
ROUND Function
• If the number of decimal places is a positive number, the number is rounded to
that number of decimal places
5
ROUND Function
• If the number of decimal places is a negative number, numbers to the left of the
decimal are rounded
6
DECIMAL Function
• DECIMAL( numeric_value ), precision, scale)
– Precision – number of digits in the result number
– Scale – number of positions after the decimal point
7
ROUND & DECIMAL Functions Together
• The ROUND and DECIMAL functions can be used in combination
• ROUND a numeric value
• Then have the rounded number returned with the required
number of decimals
8
ROUND & DECIMAL
Functions Together
9
TRUNCATE or TRUNC Function
• Truncate a specified value
• Can be used with numbers and dates
• Do not round the number, but simply truncates the number at
the specified point
• If the number of decimal places is not specified or is specified as
zero, the specified number defaults to zero
TRUNCATE(column|expression, decimal_places)
TRUNC(column|expression, decimal_places)
10
TRUNCATE or TRUNC Function
11
MOD Function
• Returns the remainder from a division operation
• Divides the first argument by the second argument and returns
the remainder
12
MOD Function
• Can be used to determine whether a value is odd or even
• If a value is divided by 2 and there is no remainder, the number
must be an even number
13
MOD Function
14
CEILING (CEIL) Function
• Returns the smallest integer value greater than or equal to a
number or numeric-expression
• Rounds up to the next highest integer value
• Argument must be a numeric value or numeric expression that
returns a value of a numeric data type
15
CEILING (CEIL) Function
16
FLOOR Function
• Returns the largest integer value less than or equal to a number
or numeric-expression
• Rounds down to the next lowest integer value
• Argument must be a numeric value or an expression that returns
a value of a numeric data type
17
FLOOR Function
18
FLOOR Function
19
ABS Function
• Returns the absolute, or positive value of the numeric values
supplied by the argument
20
SQL
Date Functions
Date Format
• The default format for dates is *ISO
– YYYY-MM-DD -- that is, 2025-01-15
• Dates are stored internally with a numeric format representing
century, year, month, day, hour, minute, and second
22
CURRENT_DATE Function
• CURRENT_DATE function returns the current date
• CURRENT DATE without underscore will work on some DBMS
23
Working with Dates
• YEAR( '2025-03-30' ); -- Returns 2025
• MONTH( '2025-03-30' ) -- Returns 3
• DAY( '2025-03-30' ) -- Returns 30
• WEEK( '2025-03-30' ) -- Returns 14
24
Working with Dates
• DAYOFWEEK( '2025-03-30' ) -- Returns 1
• DAYOFYEAR( '2025-03-30' ) -- Returns 89
• DAYNAME( '2025-03-30' ) -- Returns Sunday
• MONTHNAME( '2025-03-30' ) -- Returns March
25
EXTRACT Function
• Extracts a single component from a date
26
Julian Date Function
• Returns an integer value representing the number of days from
January 1, 4712 B.C. (the start of Julian date calendar) to the
date value specified in the argument
27
DAYS Function
• Returns an integer representation of a date from January 1,
0001
28
DAYS Function
29
DAYS Function
30
+ MONTHS
• YEAR/YEARS, MONTH/MONTHS, and DAY/DAYS functions
perform addition and subtraction on dates
• Returns a date
• If the number is negative, the number of years, months, or days
are is subtracted from the date
31
YEARS, MONTHS, & DAYS Functions
32
MONTHS_BETWEEN Function
• Returns a numeric value of the number of months between two
dates
• If the first date is earlier than the second date, a negative
number is returned
33
MONTHS_BETWEEN Function
34
MONTHS_BETWEEN Function
35
MONTHS_BETWEEN Function
36
ADD_MONTHS Function
• Add a specified number of months to a date
• Returns a date
• If the number is negative, the number of months is subtracted
from the date
• The ADD_MONTH function has an unusual characteristic as
stated here:
– "If date is the last day of the month or if the resulting month has fewer
days than the day component of date, then the result is the last day of
the resulting month. Otherwise, the result has the same day
component as date." 37
ADD_MONTHS Function
38
NEXT_DAY Function
• Returns the date of the next occurrence of a specified day of the
week after a given date
39
NEXT_DAY Function
40
LAST_DAY Function
• Returns the last day of the month from a given date
41
LAST_DAY Function
42
LAST_DAY + 1
• Returns first day of the next month from a given date
43
First Day of Next Month
LAST_DAY + 1
44
Nested Example
SELECT employee_id, hire_date,
DECIMAL( ( MONTHS_BETWEEN(CURRENT_DATE, hire_date) /12 ), 4,2) AS years_of_service,
ADD_MONTHS (hire_date, 6) AS review_date,
NEXT_DAY(hire_date, 'FRIDAY') AS NEXT_FRIDAY,
LAST_DAY(hire_date) AS LAST_DAY
FROM employees
WHERE MONTHS_BETWEEN (CURRENT_DATE, hire_date) > 36;
45
SQL
Conversion Functions
TO_CHAR Function
• Typecast numeric and date input to a character string
47
Format Dates with TO_CHAR Function
• Convert dates stored in the default *ISO YYYY-MM-DD format to
a formatted "character" string
48
Format Dates with TO_CHAR Function
49
Format Numeric Values with TO_CHAR Function
• Numeric data types do not contain formatting
– No currency signs/symbols, no commas, no decimals or other
formatting
• TO_CHAR function can be used to convert numeric values into a
formatted character string
• Rounds the numeric value to the number of decimals specified
in the format model.
51
How Functions are Evaluated
SELECT TO_CHAR(NEXT_DAY(ADD_MONTHS(hire_date, 6),'FRIDAY'),
'Day, Month DD, YYYY') AS "Next Evaluation"
FROM employees
WHERE employee_id=100;
52
Conditional Functions
CASE
COALESCE
53
Simple CASE with Selector
• Evaluates a selector and compares it to several potential values
• A selector is a variable, function, or expression that is compared
to the potential values in the WHEN clauses
– Evaluated from top to bottom
– When there is a match with the selector, the specified action in the
THEN clause is executed and the result is returned
– If no match is found, the action in the ELSE clause is applied
54
Simple CASE with Selector
55
CASE with Optional ELSE clause
• ELSE clause is optional
• Specified after all WHEN clauses
• Acts like a "catch-all" condition
56
CASE Expression
with Optional ELSE clause
57
Performing Calculations
in WHEN Clauses
58
Performing Calculations
in WHEN Clauses
59
CASE Function with Selector
in WHERE Clause
60
Searched Case Function
• Has no selector
• Evaluates multiple WHEN clauses containing Boolean
expressions and returns the first matching condition whose
value is TRUE
• If no matching condition is found among the WHERE clauses, the
action in the ELSE clause is returned
61
Searched Case Function
No ELSE Clause
62
Searched Case Function
ELSE Clause
63
COALESCE Function
• Pronounced kow-uh-les
• Coalesce means "to come together"
• Is an SQL ANSI standard function
64
COALESCE Function
• Evaluates the arguments in a list and returns the value of the
first non-null value
• If the first expression is NULL, the function continues down
the list until a non-null expression is found
• The data types of the null value column and the new value must
be the same
65
COALESCE Function
with Numeric Values
• When a numeric calculation is performed with a NULL value, the
result is NULL
• The COALESCE function converts a NULL value to a number
before numeric calculations are performed to avoid a null result
66
COALESCE Function
with Numeric Values
67
COALESCE Function
with Numeric Values
68
COALESCE Function
with Character Values
69
COALESCE Function
with Numeric and Character Values
70
71