DBMS PPT - 3
DBMS PPT - 3
Customize Output
SQL Functions
Input Output
Function
arg n
Two Types of SQL Functions
Functions
Single-row Multiple-row
functions
functions
Return one result Return one result
per row per set of rows
Single-Row Functions
Single-row functions:
• Manipulate data items
• Accept arguments and return one value
• Act on each row that is returned
• Return one result per row
• May modify the data type
• Can be nested
• Accept arguments that can be a column or an
expression
Character
functions
Case-manipulation Character-manipulation
functions functions
LOWER CONCAT
UPPER
INITCAP SUBSTR
LENGTH
INSTR
LPAD | RPAD
TRIM
REPLACE
Case-Manipulation Functions
1
SELECT employee_id, CONCAT(first_name, last_name) NAME,
job_id, LENGTH (last_name), 2
INSTR(last_name, 'a') "Contains 'a'?"
FROM employees 3
WHERE SUBSTR(job_id, 4) = 'REP';
1 2 3
Number Functions
1 2
SELECT ROUND(45.923,2), ROUND(45.923,0) ,
ROUND(45.923,-1) 3
FROM DUAL;
1 2
3
1 2
SELECT TRUNC(45.923,2), TRUNC(45.923),
TRUNC(45.923,-1) 3
FROM DUAL;
1 2 3
Using the MOD
Function
Function Result
MONTHS_BETWEEN Number of months between two dates
ADD_MONTHS Add calendar months to date
NEXT_DAY Next day of the date specified
LAST_DAY Last day of the month
ROUND Round date
TRUNC Truncate date
Using Date Functions
Function Result
MONTHS_BETWEEN 19.6774194
('01-SEP-95','11-JAN-94')
ADD_MONTHS ('11-JAN-94',6) '11-JUL-94'
NEXT_DAY ('01-SEP-95','FRIDAY') '08-SEP-95'
LAST_DAY ('01-FEB-95') '28-FEB-95'
Using Date Functions