(CSE3083) Lab Practical Assignment #6
(CSE3083) Lab Practical Assignment #6
6
Environment: Microsoft Windows
Tools/ Language: Oracle/SQL
Oracle functions:
Functions make the basic query block more powerful and are used to manipulate data values.
Functions accept one or more arguments and return one value. An argument is a user supplied
constant, variable or column reference, which can be passed to a function in the following
format:
function _name (argument1, argument2 ..)
Functions can be used to
Perform calculations on data
Modify individual data item
Manipulate output for group of rows
Alter date formats for display
Convert column data types
ORACLE TABLE
DUAL: Dual table is owned by SYS. SYS owns the data dictionary; in DUAL is a part of
data dictionary. Dual is a small Oracle worktable which consists of only one row and one
column and contains the value x in that column. Besides arithmetic calculations, it also
supports date retrieval and its formatting.
DESC DUAL;
Name Null? Type
----------------------------------------- -------- ----------------------------
DUMMY VARCHAR2(1)
Select * from DUAL
D
--
X
(a) Character Functions: accept character data as input and can return both character and
number values.
LOWER (col/value)
UPPER (col/value)
INITCAP (col/value)
SUBSTR (col/value, position, n)
INSTR (col/value, ‘string’)
ASCII (character)
CHR (number)
(c) Date Functions: Date functions are used to manipulate and extract values from the date
column of a table.
SYSDATE
ADD_MONTHS (date, count)
LAST_DAY (date)
MONTHS_BETWEEN (date2, date1)
NEXT_DAY (date, ‘day’)
NUMERIC FUNCTIONS
1. ROUND
Select ROUND(SUM(GPA)/COUNT(GPA)) as “Round Average”
from student
Round Average
--------------------
4
Round(n, m) return rounded to m places to the right of a decimal point. If m is omitted then n
is rounded to 0 places.
Select ROUND(15.81,1), round(158.158),round(158.158,-2) from
DUAL
ROUND(15.81,1)
--------------
15.8
ABS(-15)
----------
15
7. MOD: Return the remainder of first number divided by second number passed as a
parameter
Select MOD(15,7) from DUAL
MOD(15,7)
----------
1
8. TRUNCATE: Returns a truncated number to a certain number of decimal places
TRUNC(number, decimal places)
Select TRUNC(125.815,1) from dual
TRUNC(125.815,1)
----------------
125.8
9. FLOOR: Returns the largest value that is equal to or less than the number.
Select floor(24.92), floor(3.1) from dual
FLOOR(24.92) FLOOR(3.1)
------------ ----------
24 3
10. CEILING: Return the largest value that is equal to or greater than the number.
Select ceil(24.92), ceil(3.1) from dual
CEIL(24.92) CEIL(3.1)
----------- ----------
25 4
STRING FUNCTIONS:
1. Lower: Returns char, with letters in lower case.
Select LOWER(‘RAJeEv’) from dual
LOWER(
------
Rajeev
3. SUBSTRING:
SUBSTR(string, start_pos, length)
Select SUBSTR(‘Prateek’, 4, 3) from dual
SUB
---
tee
INSTR
---------
2
TO_CHAR(DA
----------
20/02/2014
8. TO_DATE:
Select to_date(‘23/02/1988’, ‘DD/MM/YYYY’) from DUAL
TO_DATE('
---------
23-FEB-88
9. ADD_MONTHS:
Select add_months(sysdate,4) from dual
ADD_MONTH
---------
17-JUN-14
ADD_MONTH
---------
20-JAN-95
10. LAST_DAY
Select sysdate, last_day(sysdate) from dual
SYSDATE LAST_DAY(
--------- ---------
17-FEB-14 28-FEB-14
s
11. NEXT_DAY
SYSDATE NEXT_DAY(
--------- ---------
20-FEB-14 21-SEP-14
Practical Assignment - 6
1 2 3 4 5 6 7 8
Q14. Display sID, sname and DoB in this format ‘February 26, 2014’
Q15. Convert the text ’26/02/2014’ to date.
Q16. Compute on which date is next Saturday and last day of this month?
Exercise
Q1. Display sID, sname and DoB in this format ‘26th Feburary, 2014’
Q2. Display sID, sname and DoB in this format ‘26/02/2014’
Q3. Add 5 months to DoB of Edward?
Q4. Display last day of DoB of Amy?
Q5. Display next Sunday of DoB of Doris?