Database Management System (DBMS - 204) : Single Row Functions
Database Management System (DBMS - 204) : Single Row Functions
(DBMS – 204)
Experiment # 03
Student Name:
Roll Number:
Experiment evaluated by
SQL Functions
1. Input Output Function
Function performs action arg 1 arg 2 Result value arg n
Note:
Most of the functions described in this lesson are specific to Oracle Corporation’s version of
SQL.
Two Types of SQL Functions
1. Single-row functions
2. Character Functions
• Case-manipulation functions
Character-manipulation functions
• CONCAT
• UPPER
• SUBSTR
• INITCAP
• LENGTH
• INSTR
• LPAD | RPAD
• TRIM
• REPLACE
Note: The functions discussed in this lesson are only some of the available functions.
QUERY
SELECT empno, ename, deptno FROM emp WHERE ename = 'smith';
no rows selected
SELECT empno, ename, deptno FROM emp WHERE LOWER(ename) = 'smith`';
Character-Manipulation Functions
These functions manipulate character strings:
Function RESULT
CONCAT('Hello', 'World') HelloWorld
SUBSTR('HelloWorld',1,5) Hello
LENGTH('HelloWorld') 10
INSTR('HelloWorld', 'W') 6
LPAD(sal,10,'*') *****24000
RPAD(sal, 10, '*') 24000*****
TRIM('H' FROM 'HelloWorld') elloWorld
QUERY
SELECT empno, CONCAT(ename, job) NAME, sal,
LENGTH (ename), INSTR(ename, 'A') "Contains 'a'?"
FROM emp WHERE SUBSTR(job, 1,3) = 'MAN';
Number Functions
• ROUND : Rounds value to specified decimal
ROUND(45.926, 2) 45.93
• TRUNC : Truncates value to specified decimal
TRUNC(45.926, 2) 45.92
• MOD : Returns remainder of division
MOD(1600, 300) 100
QUERY (ROUND)
SELECT ROUND(45.923,2), ROUND(45.923,0),ROUND(45.923,-1)
FROM DUAL;
DUAL is a dummy table you can use to view results
from functions and calculations.
QUERY (TRUNC)
SELECT TRUNC(45.923,2), TRUNC(45.923), TRUNC(45.923,-2) FROM DUAL;
• Subtract two dates to find the number of days between those dates.
date - date Number of days Subtracts one date from another
Note:SYSDATE is a SQL function that returns the current date and time. Your results may
differ from the example. If a more current date is subtracted from an older date, the difference
is a negative number.
Using Date Functions
• MONTHS_BETWEEN ('01-SEP-95','11-JAN-94')
19.6774194
• 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'
Example
Compare the hire dates for all employees who started in 1981. Display the employee number,
hire date, and month started using the ROUND and TRUNC functions.
Conversion Functions
Data-type conversion
Implicit data-type conversion
Note: CHAR to NUMBER conversions succeed only if the character string represents a valid
number.
Explicit Data-Type Conversion
1. TO_NUMBER
2. TO_DATE
3. NUMBER
4. DATE
5. CHARACTER
6. TO_CHAR
Function Purpose
TO_CHAR( number | date ,[ fmt ], Converts a number or date value to a
VARCHAR2 [nlsparams ]) character string with format model fmt.
TO_NUMBER( char,[fmt], Converts a character string containing digits to a
[nlsparams] ) number in the format specified by
the optional format model Fmt . The nlsparams
parameter has the same purpose in this function
as in the TO_CHAR function for number
conversion.
TO_DATE(char ,[ fmt ],[ nlsparams ]) Converts a character string representing a date to
a date value according to the fmt specified. If fmt
is omitted, the format is DD-MON-YY.The
nlsparams parameter has the same purpose in this
function as in the TO_CHAR function for date
conversion.
Note: The list of functions mentioned in this lesson includes only some of the available
conversion functions.
Nesting Functions
• Single-row functions can be nested to any level.
• Nested functions are evaluated from deepest level to the least deep level.
F3(F2(F1(col,arg1),arg2),arg3)
Step 1 = Result 1
Step 2 = Result 2
Step 3 = Result 3
Nesting Functions
Single-row functions can be nested to any depth. Nested functions are evaluated from the
innermost level to the outermost level. Some examples follow to show you the flexibility of
these functions.
Example
Display the date of the next Friday that is six months from the hire date. The resulting date
should appear as Friday, August 13th, 1999. Order the results by hire date.
SELECT hiredate,TO_CHAR(NEXT_DAY(ADD_MONTHS(hiredate, 6), 'FRIDAY'),
'fmDay, Month DDth, YYYY')
"Next 6 Month Review"
FROM emp
ORDER BY hiredate;
General Functions
These functions work with any data type and pertain to using null value.
• NVL (expr1, expr2)
• NVL2 (expr1, expr2, expr3)
• NULLIF (expr1, expr2)
•COALESCE (expr1, expr2, ..., exprn)
Function Description
NVL Converts a null value to an actual value
NVL2 If expr1 is not null, NVL2 returns expr2.
NULLIF If expr1 is null, NVL2 returns. The argument can have any
data type. expr3 expr1
COALESCE Compares two expressions and returns null if they are equal,
or the first expression if they are not equal Returns the first
non-null expression in the expression list
NVL Function
• Converts a null to an actual value
• Data types that can be used are date, character,and number.
• Data types must match:
– NVL(commission,0)
– NVL(hiredate,'01-JAN-97')
– NVL(job,'No Job Yet')
NVL Conversions for Various Data Types
Data Type Conversion Example
NUMBER NVL( number_column ,9)
DATE NVL( date_column, '01-JAN-95')
CHAR or VARCHAR2 NVL( character_column , 'Unavailable')
Practice 2, Part 2
For each employee, display the employee’s name, and calculate the number of months
between today and the date the employee was hired. Label the column
MONTHS_WORKED . Order your results by the number of months employed. Round the
number of months up to the closest whole number.
Note:
Your results will differ.
4. Display the name, hiredate, and day of the week on which the employee started. Label
the column DAY . Order the results by the day of the week starting with Monday.
5. Create a query that displays the employees’ names and commission amounts. If an
employee does not earn commission, put “No Commission.” Label the column
COMM.
6. Create a query that displays the employees’ names and indicates the amounts of their
annual salaries with asterisks. Each asterisk signifies a thousand dollars. Sort the data
in descending order of salary. Label the column
EMPLOYEES_AND_THEIR_SALARIES.