SINGLE ROW FUNCTIONS
SESSION 15-16
SUBJECT MATTER EXPERT
D3408 – Mediana Aryuni
ACKNOWLEDGEMENT
These slides have been adapted from
• Carlos Coronel and Steven Morris.
(2023). Database Systems: Design,
Implementation, and Management.
Cengage Learning. Boston. ISBN:
9780357673034. Chapter 7.
• Oracle Academy, Database
Programming with SQL
LEARNING OUTCOMES
•LO-3: Apply Structured Query Language that is
suitable for the problem
OUTLINES
Introduction to Functions
Case and Character Manipulation
Number Functions
Date Functions
Conversion Functions
NULL Functions
Conditional Expressions
INTRODUCTION TO
FUNCTIONS
DATABASE MODEL (1)
DATABASE MODEL (2)
VENDOR AND PRODUCT TABLES
SQL FUNCTIONS
• Generating information from data often requires many data manipulations.
• Sometimes such data manipulation involves the decomposition of data elements.
• For example, an employee’s date of birth can be subdivided into a day, a month, and a year.
• SQL functions are very useful tools.
• Functions always use a numeric, date, or string value.
• The value may be part of the command itself (a constant or literal), or it may be an attribute
located in a table.
• Therefore, a function may appear anywhere in a SQL statement where a value or an attribute
can be used (SELECT, WHERE, and ORDER BY clauses).
• There are many types of SQL functions, such as arithmetic, trigonometric, string, date, and
time functions.
DUAL TABLE
• The DUAL table has one row called "X" and one column called "DUMMY"
• The DUAL table is used to create SELECT statements and execute functions
not directly related to a specific database table
• Queries using the DUAL table return one row as a result.
• DUAL can be useful to do calculations and also to evaluate expressions that
are not derived from a table
• SELECT (319/29) + 12 FROM DUAL;
CASE AND CHARACTER
MANIPULATION
CASE AND CHARACTER MANIPULATION
LOWER
• LOWER(column | expression) converts alpha characters to lower-case
SELECT last_name
FROM employees
WHERE LOWER(last_name) = 'abel';
UPPER & INITCAP
• UPPER(column | expression) converts alpha characters to upper-case
SELECT last_name
FROM employees
WHERE UPPER(last_name) = 'ABEL';
• INITCAP(column | expression) converts alpha character values to uppercase
for the first letter of each word
SELECT last_name
FROM employees
WHERE INITCAP(last_name) = 'Abel';
CONCAT
• CONCAT: Joins two values together
• Takes 2 character string arguments, and joins the second string to the first.
• Could also be written using the concatenation operator -'Hello' || 'World'
SELECT CONCAT('Hello', 'World') FROM DUAL;
SELECT CONCAT(first_name, last_name) FROM employees;
SUBSTR
• SUBSTR: Extracts a string of a determined length
• The arguments are (character String, starting position, length)
• The Length argument is optional, and if omitted, returns all characters to the
end of the string
SELECT SUBSTR('HelloWorld', 1, 5) FROM DUAL; Hello
SELECT SUBSTR('HelloWorld', 6) FROM DUAL; World
SELECT SUBSTR(last_name, 1, 3) FROM employees;
LENGTH
• LENGTH: Shows the length of a string as a number value
• The function takes a character string as an argument, and returns the
number of characters in that character string
• Examples:
SELECT LENGTH('HelloWorld') FROM DUAL; 10
SELECT LENGTH(last_name) FROM employees;
INSTR
• INSTR: Finds the numeric position of the specified character(s)
• INSTR searches for the first occurrence of a substring within a character
string and returns the position as a number
• If the substring is not found, the number zero is returned
• Examples:
SELECT INSTR('HelloWorld', 'W') FROM DUAL; 6
SELECT last_name, INSTR(last_name, 'a') FROM employees;
LPAD
• LPAD: Pads the left side of a character string, resulting in a right-justified
value
• LPAD requires 3 arguments: a character string, the total number of
characters in the padded string, and the character to pad with
• Examples:
SELECT LPAD('HelloWorld', 15, '-') FROM DUAL; -----HelloWorld
SELECT LPAD(last_name, 10, '*') FROM employees;
RPAD
• RPAD: Pads the right-hand side of a character string, resulting in a left-
justified value
• Examples:
SELECT RPAD('HelloWorld', 15, '-') FROM DUAL; HelloWorld-----
SELECT RPAD(last_name, 10, '*') FROM employees;
TRIM
• TRIM: Removes all specified characters from either the beginning, the end,
or both beginning and end of a string
REPLACE
• REPLACE: Replaces a sequence of characters in a string with another set of
characters
• The syntax for the REPLACE function is:
REPLACE (string1, string_to_replace, [replacement_string] )
−string1 is the string that will have characters replaced in it
−string_to_replace is the string that will be searched for and taken out of
string1 −[replacement_string] is the new string to be inserted in string1
STRING FUNCTIONS
STRING FUNCTIONS
STRING FUNCTIONS
NUMBER FUNCTIONS
NUMBER FUNCTIONS
ROUND
• ROUND can be used with both numbers and dates.
• It is mainly used to round numbers to a specified number of decimal places, but it
can also be used to round numbers to the left of the decimal point.
• Syntax: ROUND(column|expression, decimal places)
TRUNC
• The TRUNC function can be used with both numbers and dates.
• It is mainly used to terminate the column, expression, or value to a specified
number of decimal places.
• When TRUNC is used, if the number of decimal places is not specified, then like
ROUND, the specified number defaults to zero.
• Syntax: TRUNC(column|expression, decimal places)
MOD
• The MOD function finds the remainder after one value is divided by another value
ROUND
• If the number of decimal places is a positive number, the number is rounded
to that number of decimal places to the right of the decimal point
• ROUND(45.926, 2) 45.93
• If the number of decimal places is a negative number, the number is
rounded to that number of decimal places to the left of the decimal point
• ROUND(45.926, -1) 50
TRUNC
• TRUNC (45.926, 2) 45.92
• As with ROUND, if the TRUNC expression does not specify the number of
decimal places or specifies a zero, the number is truncated to zero decimal
places
• TRUNC (45.926, 0) 45
• TRUNC (45.926) 45
• Remember that TRUNC does not round the number
• It simply terminates the number at a given point
MOD
• MOD can be used to determine whether a value is odd or even.
• If you divide a value by 2 and there is no remainder, the number must be an
even number
• For example, if the MOD of x divided by 2 is 0, then x must be an even
number
SELECT country_name, MOD(airports,2) AS "Mod Demo" FROM wf_countries;
• 1 means the number is odd, and zero means that it is even
NUMERIC FUNCTIONS
DATE FUNCTIONS
DISPLAYING DATES
• The default display and input format for dates is:
− DD-Mon-YYYY
• For example: 02-Dec-2014
• However, the Oracle database stores dates internally with a numeric
format representing the century, year, month, day, hour, minute, and
second
• Valid Oracle dates are between January 1, 4712 B.C., and December 31,
9999 A.D.
• This represents the range of dates that you can store successfully in an
Oracle database
SYSDATE
• SYSDATE is a date function that returns the current database server date
and time
• Use SYSDATE to display the current date, use the DUAL table
SELECT SYSDATE FROM dual;
WORKING WITH DATES
DATE FUNCTIONS
• The date functions shown in the table operate on Oracle dates
• All of the date functions return a value with a DATE data type except the
MONTHS_BETWEEN function, which returns a numeric data type value
MONTHS_BETWEEN
• MONTHS_BETWEEN: takes 2 DATE arguments and returns the number of
calendar months between the 2 dates
• If the first argument is an earlier date than the second, the number returned
is negative
SELECT last_name, hire_date FROM employees
WHERE MONTHS_BETWEEN (SYSDATE, hire_date) > 240;
ADD_MONTHS
• ADD_MONTHS: takes 2 arguments, a DATE and a number
• Returns a DATE value with the number argument added to the month
component of the date
• If the number supplied is negative, the function will subtract that number of
months from the date argument
SELECT SYSDATE, ADD_MONTHS (SYSDATE, 12) AS "Next Year"
FROM dual;
NEXT_DAY
• NEXT_DAY: takes 2 arguments, a DATE and a weekday and returns the
DATE of the next occurrence of that weekday after the DATE argument
SELECT SYSDATE, NEXT_DAY (SYSDATE, 'Saturday') AS "Next Saturday"
FROM dual;
LAST_DAY
• LAST_DAY: takes a DATE argument and returns the DATE of the last day of
the month for the DATE argument
SELECT SYSDATE, LAST_DAY (SYSDATE) AS "End of the Month"
FROM dual;
ROUND
• ROUND: returns a DATE rounded to the unit specified by the second
argument
TRUNC
• TRUNC: returns a DATE truncated to the unit specified by the second
argument
DATE FUNCTIONS
SELECT employee_id, hire_date, ROUND(MONTHS_BETWEEN(SYSDATE,
hire_date)) AS TENURE, ADD_MONTHS (hire_date, 6) AS REVIEW,
NEXT_DAY(hire_date, 'FRIDAY'), LAST_DAY(hire_date)
FROM employees
WHERE MONTHS_BETWEEN (SYSDATE, hire_date) > 36;
ORACLE DATE/TIME FUNCTIONS
ORACLE DATE/TIME FUNCTIONS
ORACLE DATE/TIME FUNCTIONS
CONVERSION FUNCTIONS
DATA TYPES DESCRIBED
• VARCHAR2: Used for character data of variable length, including numbers,
dashes, and special characters
• CHAR: Used for text and character data of fixed length, including numbers,
dashes, and special characters
• NUMBER: Used to store variable-length numeric data. No dashes, text, or
other nonnumeric data are allowed Currency is stored as a number data
type
• DATE: Used for date and time values. Internally, Oracle stores dates as
numbers and, by default, DATE information is displayed as DD-Mon-YYYY (for
example, 23-Oct-2013)
EXPLICIT DATA TYPE
CONVERSION
DATE CONVERSION TO CHARACTER DATA
• Use sp to spell out a number
• Use th to have the number appear
as an ordinal −(1st, 2nd, 3rd, and
so on)
• Use an fm element to remove
padded blanks or remove leading
zeroes from the output
DATE CONVERSION TO CHARACTER DATA
NUMBER CONVERSION TO CHARACTER DATA
(VARCHAR2)
NUMBER CONVERSION TO CHARACTER DATA
(VARCHAR2)
CHARACTER CONVERSION TO NUMBER
SELECT TO_NUMBER('5,320', '9,999') AS "Number"
FROM dual;
SELECT last_name, TO_NUMBER(bonus, '9999') AS "Bonus"
FROM employees
WHERE department_id = 80;
CHARACTER CONVERSION TO
DATE
• When making a character-to-date conversion, the fx (format exact) modifier
specifies exact matching for the character argument and the date format
model
SELECT TO_DATE('May10,1989', 'fxMonDD,YYYY') AS "Convert" FROM DUAL;
CONVERSION FUNCTIONS
CONVERSION FUNCTIONS
CONVERSION FUNCTIONS
RR DATE FORMAT AND YY DATE FORMAT
• If the date format is specified with the RR
format, the return value has two
possibilities, depending on the current
year
NULL FUNCTIONS
NULL FUNCTIONS
• NVL: The NVL function converts a null value to a known value of a fixed data type,
either date, character, or number
• The data types of the null value column and the new value must be the same
• The NVL function is: NVL (expression 1 value that may contain a null, expression 2 value
to substitute for null)
• SELECT last_name, NVL(commission_pct, 0) FROM employees
NULL FUNCTIONS
• NVL2: The NVL2 function evaluates an expression with three values.
• If the first value is not null, then the NVL2 function returns the second expression
• If the first value is null, then the third expression is returned.
• The NVL2 function is: NVL2 (expression 1 value that may contain a null, expression 2
value to return if expression 1 is not null, expression 3 value to replace if expression 1 is
null)
• SELECT last_name, NVL2(commission_pct, salary*commission_pct, 0) FROM employees
NULL FUNCTIONS
• NULLIF: The NULLIF function compares two expressions
• If they are equal, the function returns null
• If they are not equal, the function returns the first expression
• The NULLIF function is: NULLIF(expression 1, expression 2)
• SELECT emp.last_name, mgr.last_name, emp.department_id, mgr.department_id,
NULLIF(emp.department_id, mgr.department_id) FROM employees emp, employees mgr
WHERE emp.manager_id=mgr.employee_id
NULL FUNCTIONS
• COALESCE: is an extension of the NVL function, except COALESCE can take multiple values
• The word "coalesce" literally means "to come together" and that is what happens
• If the first expression is null, the function continues down the line until a not null expression is found.
Of course, if the first expression has a value, the function returns the first expression and the function
stops
• The COALESCE function is: COALESCE (expression 1, expression 2, ...expression n)
• SELECT last_name, commission_pct, department_id, manager_id, COALESCE(commission_pct,
department_id, manager_id) FROM employees
CONDITIONAL
EXPRESSIONS
CONDITIONAL EXPRESSIONS
• CASE and DECODE are examples of one of these differences
• CASE is an ANSI/ISO 99 SQL 99 compliant statement
• DECODE is an Oracle Proprietary statement
CONDITIONAL EXPRESSIONS
• CASE
SELECT last_name, department_id, CASE department_id WHEN 90 THEN 'Executive
Board' ELSE 'Non Executive' END FROM employees
• DECODE
SELECT last_name, department_id, DECODE(department_id, 90, 'Executive Board',
'Non Executive') FROM employees
EXERCISE
EXERCISE
• Based on the ERD, create SELECT statements for
EMPLOYEES table using:
1. Character Function
2. Number Function
3. Date Function
4. Conversion Function
5. NULL Function
6. Conditional Expression
REFERENCES
• Carlos Coronel and Steven Morris. (2023). Database Systems: Design,
Implementation, and Management. Cengage Learning. Boston. ISBN:
9780357673034.
• Oracle Academy, Database Programming with SQL
THANK YOU
schoolisbinu SIS @schoolisbinus
s Binus
http://www.facebook.com/
schoolisbinus
http:// schoolisbinus@binus.a
sis.binus.ac.id c.id