Quary 4
Quary 4
Quary 4
Utpal/SCA/SQL-3 1
SQL Function
arg2
arg n Result
Value
Utpal/SCA/SQL-3 2
Function
Single-row Multiple-row
function function
Utpal/SCA/SQL-3 3
Single Row Functions
Utpal/SCA/SQL-3 4
Single Row Functions
Character
Single-row
General Number
Functions
Conversion Date
Utpal/SCA/SQL-3 5
Character Functions
Character
Function
Function Result
LOWER(‘GOD Bless You’) god bless you
Utpal/SCA/SQL-3 7
Example:
SQL> SELECT ‘The job title for ’ || INITCAP (ename)||‘ is ’
|| LOWER(job) AS “EMPLOYEE DETAILS”
FROM emp;
Sample Output:
EMPLOYEE DETAILS
-----------------------------------------------------------------------
The job title for King is president
The job title for Blake is manager
The job title for Clark is manager
...
14 rows selected.
Utpal/SCA/SQL-3 8
Character Manipulation function
Function Result
CONCAT(‘Good’,‘morning’) Goodmorning
SUBSTR(‘KIIT’,2,3) IIT
LENGTH(‘UTPAL’) 5
INSTR(‘Utpal’, ’a’) 4
LPAD(sal,10,’x’) xxxxxx5000
Sample Output:
Utpal/SCA/SQL-3 10
Number Functions
Function Result
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
MOD(1600,300) division
100
Utpal/SCA/SQL-3 11
Example:
SQL> SELECT ROUND(45.923 , 2), ROUND(45.923 , 0) ,
ROUND(45.923, -1)
FROM DUAL;
Sample Output:
Utpal/SCA/SQL-3 12
Example:
SQL> SELECT TRUNC(45.923 , 2), TRUNC(45.923 , 0) ,
TRUNC(45.923, -1)
FROM DUAL;
Sample Output:
Utpal/SCA/SQL-3 13
Using the MOD Functions
Example:
SQL> SELECT ename, sal, comm, MOD(sal, comm)
FROM emp
WHERE job = ‘SALESMAN’;
Sample Output:
Utpal/SCA/SQL-3 14
Working with Dates
• Oracle stores dates in an internal character
format : century, year, month, day, hours,
minutes, seconds.
• The default date format is DD-Mon-YY.
• SYSDATE is function returning date and
time. However when the function is
executed it only gives the date, to view the
time part it needs to be converted to
character.
• DUAL is a dummy table used to view
Utpal/SCA/SQL-3 15
Dual
DUAL table is owned by SYS and can be
accessed by all users. As SELECT and
FROM clause are mandatory and several
calculations do not need to use actual
tables.
Example:
SQL> SELECT sysdate
FROM dual;
3 – SEP - 01
Utpal/SCA/SQL-3 16
Arithmetic with Dates
Utpal/SCA/SQL-3 17
Arithmetic with Dates
Example:
SQL> SELECT last_name,(SYSDATE – hire_date) / 7 WEEKS
FROM employees
WHERE department_id = 90;
Sample Output:
ENAME WEEKS
------------ -----------------------
KING 830.93709
CLARK 853.93709
MILLER 321.36566
Utpal/SCA/SQL-3 19
Date functions
Function Description
Number of months
MONTHS_BETWEEN
between two dates
Add calendar months
ADD_MONTHS
to date
Next day of the date
NEXT_DAY
specified
LAST_DAY Last day of the month
• 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-SEP-95’)
’30-SEP-95’
Utpal/SCA/SQL-3 21
Example:
SQL> SELECT empno, hiredate,
MONTHS_BETWEEN(TO_DATE(SYSDATE,’DD-MON-YY’),hiredate)
TENURE,
ADD_MONTHS(hiredate, 6) REVIEW,
NEXT_DAY(hiredate, ‘FRIDAY’),
LAST_DAY(hiredate)
FROM emp
WHERE MONTHS_BETWEEN(TO_DATE(SYSDATE,’DD-MON-YY’),
,hiredate) < 200;
Sample Output:
EMPNO HIREDATE TENURE REVIEW NEXT_DAY( LAST_DAY(
----------- ----------------- ---------------- ----------- ------------------ -----------------
7839 17-NOV-81 192.24794 17-MAY-82 20-NOV-81 30-NOV-81
7840 01-MAY-81 198.76407 01-NOV-81 08-MAY-81 31-MAY-81
…….
11 Rows selected
Utpal/SCA/SQL-3 22
Using Date Functions
• ROUND(’25-JUL-95’,’MONTH’) 01-AUG-95
• ROUND(’25-JUL-95’,’YEAR’) 01-JAN-96
• TRUNC(’25-JUL-95’,’MONTH’) 01-JUL-95
• TRUNC(’25-JUL-95’,’YEAR’) 01-JAN-95
Utpal/SCA/SQL-3 23
Example:
SQL> SELECT empno, hiredate,
ROUND(hiredate,’MONTH’) , TRUNC(hiredate,’MONTH’)
FROM emp
WHERE hiredate like ‘%1982’;
Sample Output:
EMPNO HIREDATE ROUND(HIR TRUNC(HIR
----------- ----------------- -------------------
------------------
7788 09-DEC-82 01-DEC-82 01-DEC-82
7934 23-JAN-82 01-FEB-82 01-JAN-82
…….
Utpal/SCA/SQL-3 24
Conversion Functions
Data type
Conversion
Utpal/SCA/SQL-3 25
Implicit data type conversion
For assignments Oracle Server can automatically convert
the following:
From To
VARCHAR2 or CHAR NUMBER
VARCHAR2 or CHAR DATE
NUMBER VARCHAR2
DATE VARCHAR2
From To
VARCHAR2 or CHAR NUMBER
VARCHAR2 or CHAR DATE
Utpal/SCA/SQL-3 26
Explicit data type conversion
TO_NUMBER TO_DATE
TO_CHAR TO_CHAR
Utpal/SCA/SQL-3 27
TO_CHAR Function with dates
TO_CHAR(date, ‘fmt’)
The format model :
•Must be enclosed in single quotation marks and is case
sensitive.
• Can include any valid date format element.
• Has an fm (fill mode) element to remove padded blanks or
suppress leading zeros.
• Is separated from the date value by a comma.
Example:
SQL> SELECT empno, TO_CHAR(hiredate, ‘fmMM/YY’) Month_Hired
FROM emp
WHERE ename = ‘BLAKE’ ;
Utpal/SCA/SQL-3 28
Element of date format model
Utpal/SCA/SQL-3 29
Element Description
SCC or CC Century, S prefixes BC date with -
Years in dates YYYY or Year, S prefixes BC date with -
SYYYY
YYY or YY or Y Last 3 / 2 / 1 digit of the year
Y,YYY Year with comma in this position
IYYY,IYY,IY,I 4/3/2/1 digit year based on the ISO
standard
SYEAR or YEAR Year spelled out S prefixes BC
date with -
BC or AD BC/AD indicator
B.C or A.D BC/AD Indicator with periods
Q quarter of the year
MM Month, two digit value
Utpal/SCA/SQL-3 30
Element Description
MONTH Name of the month padded with
blanks to length of nine
characters
MON Name of the month, three letter
abbreviation.
RM Roman numeral month
WW or W Week of year or month
DDD or DD or D Day of year / month/ week
DAY Name of the day padded with
blanks to length of nine character
DY Name of the Day; three letter
abbreviation
J Julian day; the number of days
since 31 December 4731 BC
Utpal/SCA/SQL-3 31
Element Description
AM or PM Meridian indicator
A.M or P.M Meridian indicator with periods
HH / HH12 / HH24 Hour of day or hour (1-12) or hour
(0-23)
MI Minute ( 0 –59 )
SS Second ( 0 – 59 )
SSSSS Seconds past midnight ( 0 –
86399)
TH Ordinal Number ( DDTH for 4TH )
SP Spelled out number (DDSP for
FOUR)
SPTH or THSP Spelled-out ordinal number
(DDSPTH – FOURTH)
Utpal/SCA/SQL-3 32
Using TO_CHAR Function with dates
Example:
SQL> SELECT ename,
TO_CHAR(hiredate,’fmDdspth “of” Month YYYY
fmHH : MI : SS AM’) HIREDATE
FROM emp;
Sample Output:
ENAME HIREDATE
----------- ------------------------------------
KING Seventeenth of November 1981 12:00:00 AM
BLAKE First of May 1981 12 : 00 :00 AM
…….
Utpal/SCA/SQL-3 34
•Create a query to display the last name and salary for all employees
.Format the salary to be 15 characters long. Left padded with $.
Label the column SALARY.
• Create a query that display the employee last name and indicates
the amounts of their annual salary to be 8 characters with (*). Sort
the data in descending order of salary. Label the column Employee
_And _Their_ Salary.
•Create a query to display First_name, Hire date, One year
completion date and salary of all employees in ascending order
according to hire date.
•Create a query to display the Last name, salary of employee and
increased salary by 20% in department 20, 80 and 50 .
•Write a query to display the Last name, First name , total length of
their complete name, designation ,salary of each employee in
department 40 and 50 . sort the records as per their length of name.
•Write a query to display the Location ID , city, state_ province and
city code where city code is the first four character of city, in
descending order of city code Utpal/SCA/SQL-3
for country id US,UK,CA,JP and IN. 35