Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Quary 4

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 35

FUNCTIONS

Utpal/SCA/SQL-3 1
SQL Function

Input Function Output


Function
arg1 Performs action

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

• Manipulate data items


• Accept arguments and return one value
• Act on each row returned
• Return one result per row.
• May modify the data type.
• Can be nested.

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

Case Conversion Character Manipulation


function function
LOWER CONCAT
UPPER SUBSTR
INITCAP LENGTH
INSTR
L-PAD
R-PAD
Utpal/SCA/SQL-3 TRIM 6
Case Conversion function

Function Result
LOWER(‘GOD Bless You’) god bless you

UPPER(‘GOD Bless You’) GOD BLESS YOU

INITCAP(‘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

TRIM(‘K’ FROM ‘KIIT’) IIT


Utpal/SCA/SQL-3 9
Example:
SQL> SELECT ename, CONCAT(ename,job)
CONCAT, LENGTH(ename) LENGTH,
INSTR(ename,’A’) INSTR
FROM emp
WHERE SUBSTR(job,1,5) = ‘SALES’;

Sample Output:

ENAME CONCAT LENGTH INSTR


----------------- ---------------------------- ---------------- ------------------
MARTIN MARTINSALESMAN 6 2
ALLEN ALLENSALESMAN 5 1
TURNER TURNERSALESMAN 6 0
WARD WARDSALESMAN 4 2

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:

ROUND(45.923 , 2) ROUND(45.923 , 0) ROUND(45.923,


-1)
--------------------------- -------------------------- --------------------------
45.92 46 50

Utpal/SCA/SQL-3 12
Example:
SQL> SELECT TRUNC(45.923 , 2), TRUNC(45.923 , 0) ,
TRUNC(45.923, -1)
FROM DUAL;

Sample Output:

TRUNC(45.923 , 2) TRUNC(45.923 , 0) TRUNC(45.923, -1)


--------------------------- -------------------------- --------------------------
45.92 45 40

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:

ENAME SAL COMM MOD(SAL,COMM)


------------------ ----------------- --------------------- --------------------------
MARTIN 1250 1400 1250
ALLEN 1600 300 100
TURNER 1500 0 1500
WARD 1250 500 250

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

• Add or subtract a number to or from a date


for a resultant date value.
• Subtract two dates to find the number of
days between those days.
• Add hours to a date by dividing the number
of hours by 24.

Utpal/SCA/SQL-3 17
Arithmetic with Dates

Operation Result Description


Date + number Date Adds a number of
days to a date
Date – number Date Subtracts a
number of days
from a date
Date – date Number of Subtracts one date
days from another
Date + Date Adds a number of
number/24 hours to a date
Utpal/SCA/SQL-3 18
Using Arithmetic operators 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

ROUND Round date

TRUNC Truncate Date


Utpal/SCA/SQL-3 20
Using Date Formats

• 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

Implicit data type Explicit data type


conversion 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

For expression Oracle Server can automatically convert


the following:

From To
VARCHAR2 or CHAR NUMBER
VARCHAR2 or CHAR DATE
Utpal/SCA/SQL-3 26
Explicit data type conversion
TO_NUMBER TO_DATE

NUMBER CHARACTER 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

YYYY Full year in numbers


YEAR Year spelled out
MM Two-digit value for month
MONTH Full name of the month
DY Three-letter abbreviation of the day of
the week
DAY Full name of the day

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
…….

14 Rows selected Utpal/SCA/SQL-3 33


1. Display the last name, salary and commission for all employees
whose commission amount is 20%.
2. Display the employees number, last name, salary and salary
increase by 50% and expressed as a whole number.
3. W.A.Q that display the employees last name with the first letter
capitalized and all other letters in lower case and the length of the
name for all employees whose name starts with J , A or M. Give
each column an appropriate label. Sort the result by the
employee’s last name.
4. For each employee, display the employees last name, and
calculate the number of months between today and the date of
the employees 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.

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

You might also like