day1 session2
Post on 02-Apr-2018
242 Views
Preview:
TRANSCRIPT
-
7/27/2019 day1 session2
1/57
Entry Level Technology Program
Satyam Learning Center ORACLE 1 of 1
FUNCTIONS
-
7/27/2019 day1 session2
2/57
Entry Level Technology Program
Satyam Learning Center ORACLE 2 of 1
SQL Functions
FunctionInput
arg 1
arg 2
arg n
Functionperforms action
Output
Resultvalue
-
7/27/2019 day1 session2
3/57
Entry Level Technology Program
Satyam Learning Center ORACLE 3 of 1
Two Types of SQL Functions
Functions
Single-row
functions
Multiple-row
functions
-
7/27/2019 day1 session2
4/57
Entry Level Technology Program
Satyam Learning Center ORACLE 4 of 1
Aggregating DataUsing Group Functions
-
7/27/2019 day1 session2
5/57
Entry Level Technology Program
Satyam Learning Center ORACLE 5 of 1
What Are Group Functions?
Group functions operate on sets of rows togive one result per group.EMPLOYEES
The maximumsalary inthe EMPLOYEES
table.
-
7/27/2019 day1 session2
6/57
Entry Level Technology Program
Satyam Learning Center ORACLE 6 of 1
Types of Group Functions
AVG
COUNT
MAX
MIN
SUM
-
7/27/2019 day1 session2
7/57
Entry Level Technology Program
Satyam Learning Center ORACLE 7 of 1
SELECT [column,] group_function(column), ...FROM table
[WHERE condition][GROUP BY column][ORDER BY column];
Group Functions Syntax
-
7/27/2019 day1 session2
8/57
Entry Level Technology Program
Satyam Learning Center ORACLE 8 of 1
SELECT AVG(SAL), MAX(SAL),
MIN(SAL), SUM(SAL)
FROM EMP
WHERE JOB LIKE '%MAN%';
Using theAVG and SUM
Functions
You can use AVG and SUM for numeric data.
-
7/27/2019 day1 session2
9/57
Entry Level Technology Program
Satyam Learning Center ORACLE 9 of 1
Using theMIN ,MAX and Count
Functions
You can use MIN and MAX for any data type.
SELECT MIN(HIREDATE), MAX(HIREDATE)
FROM employees;
COUNT(*) returns the number of rows in a table.
SELECT COUNT(*)
FROM EMP
WHERE DEPTNO = 10;
-
7/27/2019 day1 session2
10/57
Entry Level Technology Program
Satyam Learning Center ORACLE 10 of 1
SELECT COUNT(DISTINCT DEPTNO)
FROM EMP;
Using the DISTINCT Keyword
COUNT(DISTINCT expr) returns the
number of distinct non-null values ofthe expr.
Display the number of distinctdepartment values in the EMPLOYEES
table.
-
7/27/2019 day1 session2
11/57
Entry Level Technology Program
Satyam Learning Center ORACLE 11 of 1
SELECT AVG(COMM)
FROM EMP;
Group Functions and NullValues
Group functions ignore null values in the column.
The NVL function forces group functions to include
null values.
SELECT AVG(NVL(COMM, 0))
FROM EMP;
-
7/27/2019 day1 session2
12/57
Entry Level Technology Program
Satyam Learning Center ORACLE 12 of 1
Single-Row Functions
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
Accept arguments which can be a column or anexpression
function_name [(arg1, arg2,...)]
-
7/27/2019 day1 session2
13/57
Entry Level Technology Program
Satyam Learning Center ORACLE 13 of 1
Single-Row Functions
Conversion
Character
Number
Date
General Single-rowfunctions
-
7/27/2019 day1 session2
14/57
Entry Level Technology Program
Satyam Learning Center ORACLE 14 of 1
Character Functions
Characterfunctions
LOWER
UPPER
INITCAP
CONCAT
SUBSTR
LENGTH
INSTRLPAD | RPAD
TRIM
REPLACE
Case-manipulationfunctions
Character-manipulationfunctions
-
7/27/2019 day1 session2
15/57
Entry Level Technology Program
Satyam Learning Center ORACLE 15 of 1
Function Result
Case Manipulation Functions
These functions convert case for character strings.
LOWER('SQL Course')
UPPER('SQL Course')
INITCAP('SQL Course')
sql course
SQL COURSE
Sql Course
-
7/27/2019 day1 session2
16/57
Entry Level Technology Program
Satyam Learning Center ORACLE 16 of 1
Using Case ManipulationFunctions
Display the employee number, name, and departmentnumber for employee Higgins:
SELECT EMPNO, ENAME, DEPTNOFROM EMPWHERE ENAME = smith';no rows selected
SELECT EMPNO, ENAME, DEPTNO
FROM EMPWHERE LOWER(ENAME) = smith';
-
7/27/2019 day1 session2
17/57
Entry Level Technology Program
Satyam Learning Center ORACLE 17 of 1
CONCAT('Hello', 'World')SUBSTR('HelloWorld',1,5)
LENGTH('HelloWorld')
INSTR('HelloWorld', 'W')
LPAD(salary,10,'*')RPAD(salary, 10, '*')
TRIM('H' FROM 'HelloWorld')
HelloWorldHello
10
6
*****2400024000*****
elloWorld
Function Result
Character-Manipulation Functions
These functions manipulate character strings:
-
7/27/2019 day1 session2
18/57
Entry Level Technology Program
Satyam Learning Center ORACLE 18 of 1
SELECT EMPNO, CONCAT(ENAME,JOB) NAME,
JOB, LENGTH (ENAME),
INSTR(ENAME, 'a') "Contains 'a'?"
FROM EMP
WHERE SUBSTR(JOB, 6) = MAN';
Using the Character-Manipulation Functions
-
7/27/2019 day1 session2
19/57
Entry Level Technology Program
Satyam Learning Center ORACLE 19 of 1
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
-
7/27/2019 day1 session2
20/57
Entry Level Technology Program
Satyam Learning Center ORACLE 20 of 1
SELECT ROUND(45.923,2), ROUND(45.923,0),
ROUND(45.923,-1)
FROM DUAL;
Using the ROUND Function
DUAL is a dummy table you can use to view resultsfrom functions and calculations.
-
7/27/2019 day1 session2
21/57
Entry Level Technology Program
Satyam Learning Center ORACLE 21 of 1
SELECT TRUNC(45.923,2), TRUNC(45.923),
TRUNC(45.923,-2)
FROM DUAL;
Using the TRUNC Function
-
7/27/2019 day1 session2
22/57
Entry Level Technology Program
Satyam Learning Center ORACLE 22 of 1
SELECT ENAME, SAL, MOD(SAL, 5000)
FROM EMP
WHERE JOB = 'SALESMAN';
Using theMOD Function
Calculate the remainder of a salary after it is divided by 5000for all employees whose job title is sales representative.
-
7/27/2019 day1 session2
23/57
Entry Level Technology Program
Satyam Learning Center ORACLE 23 of 1
Working with Dates
Oracle database stores dates in an internal numeric format:century, year, month, day, hours, minutes, seconds.
The default date display format is DD-MON-RR.
Allows you to store 21st century dates in the 20thcentury by specifying only the last two digits of the year.
Allows you to store 20th century dates in the 21stcentury in the same way.
SELECT ENAME, HIREDATE
FROM EMP
WHERE ENAME like 'G%';
0
-
7/27/2019 day1 session2
24/57
Entry Level Technology Program
Satyam Learning Center ORACLE 24 of 1
Working with Dates
SYSDATEis a function that returns:
Date
Time
-
7/27/2019 day1 session2
25/57
Entry Level Technology Program
Satyam Learning Center ORACLE 25 of 1
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 dates.
Add hours to a date by dividing the number of
hours by 24.
-
7/27/2019 day1 session2
26/57
Entry Level Technology Program
Satyam Learning Center ORACLE 26 of 1
Using Arithmetic Operatorswith Dates
SELECT ENAME, (SYSDATE-HIREDATE)/7 AS WEEKS
FROM EMP
WHERE DEPTNO = 20;
-
7/27/2019 day1 session2
27/57
Entry Level Technology Program
Satyam Learning Center ORACLE 27 of 1
Date Functions
Number of monthsbetween two dates
MONTHS_BETWEEN
ADD_MONTHS
NEXT_DAY
LAST_DAY
ROUND
TRUNC
Add calendar months todate
Next day of the datespecified
Last day of the month
Round date
Truncate date
Function Description
-
7/27/2019 day1 session2
28/57
Entry Level Technology Program
Satyam Learning Center ORACLE 28 of 1
MONTHS_BETWEEN ('01-SEP-95','11-JAN-94')
Using Date Functions
ADD_MONTHS ('11-JAN-94',6)
NEXT_DAY ('01-SEP-95','FRIDAY')
LAST_DAY('01-FEB-95')
19.6774194
'11-JUL-94'
'08-SEP-95'
'28-FEB-95'
-
7/27/2019 day1 session2
29/57
Entry Level Technology Program
Satyam Learning Center ORACLE 29 of 1
ROUND(SYSDATE,'MONTH') 01-AUG-95
ROUND(SYSDATE ,'YEAR') 01-JAN-96
TRUNC(SYSDATE ,'MONTH') 01-JUL-95
TRUNC(SYSDATE ,'YEAR') 01-JAN-95
Using Date Functions
Assume SYSDATE = '25-JUL-95':
-
7/27/2019 day1 session2
30/57
Entry Level Technology Program
Satyam Learning Center ORACLE 30 of 1
Conversion Functions
Implicit data typeconversion
Explicit data typeconversion
Data typeconversion
-
7/27/2019 day1 session2
31/57
Entry Level Technology Program
Satyam Learning Center ORACLE 31 of 1
Implicit Data TypeConversion
For assignments, the Oracle server can automaticallyconvert the following:
VARCHAR2 or CHAR
From To
VARCHAR2 or CHAR
NUMBER
DATE
NUMBER
DATE
VARCHAR2
VARCHAR2
-
7/27/2019 day1 session2
32/57
Entry Level Technology Program
Satyam Learning Center ORACLE 32 of 1
Implicit Data TypeConversion
For expression evaluation, the Oracle Server canautomatically convert the following:
VARCHAR2 or CHAR
From To
VARCHAR2 or CHAR
NUMBER
DATE
E li it D t T
-
7/27/2019 day1 session2
33/57
Entry Level Technology Program
Satyam Learning Center ORACLE 33 of 1
Explicit Data TypeConversion
NUMBER CHARACTER
TO_CHAR
TO_NUMBER
DATE
TO_CHAR
TO_DATE
E li it D t T
-
7/27/2019 day1 session2
34/57
Entry Level Technology Program
Satyam Learning Center ORACLE 34 of 1
Explicit Data TypeConversion
NUMBER CHARACTER
TO_CHAR
TO_NUMBER
DATE
TO_CHAR
TO_DATE
Using the TO CHAR Function
-
7/27/2019 day1 session2
35/57
Entry Level Technology Program
Satyam Learning Center ORACLE 35 of 1
Using the TO_CHARFunction
with Dates
The format model:
Must be enclosed in single quotation marks and is casesensitive
Can include any valid date format element
Has an fm element to remove padded blanks or suppress
leading zeros
Is separated from the date value by a comma
TO_CHAR(date, 'format_model')
-
7/27/2019 day1 session2
36/57
Entry Level Technology Program
Satyam Learning Center ORACLE 36 of 1
YYYY
Elements of the Date FormatModel
YEAR
MM
MONTH
DY
DAY
Full year in numbers
Year spelled out
Two-digit value for month
Three-letter abbreviation of theday of the week
Full name of the day of the week
Full name of the month
MONThree-letter abbreviation of themonth
DD Numeric day of the month
-
7/27/2019 day1 session2
37/57
Entry Level Technology Program
Satyam Learning Center ORACLE 37 of 1
Elements of the Date FormatModel
Time elements format the time portion of the date.
Add character strings by enclosing them in double quotation
marks.
Number suffixes spell out numbers.
HH24:MI:SS AM 15:45:32 PM
DD "of" MONTH 12 of OCTOBER
ddspth fourteenth
-
7/27/2019 day1 session2
38/57
Entry Level Technology Program
Satyam Learning Center ORACLE 38 of 1
Using the TO_CHARFunction
with Dates
SELECT ENAME,
TO_CHAR(HIREDATE, 'fmDD Month YYYY')
AS HIREDATE
FROM EMP;
-
7/27/2019 day1 session2
39/57
Entry Level Technology Program
Satyam Learning Center ORACLE 39 of 1
Using the TO_CHARFunction with Numbers
These are some of the format elements you can use with theTO_CHAR function to display a number value as a character:
TO_CHAR(number, 'format_model')
9
0
$
L
.
,
Represents a number
Forces a zero to be displayed
Places a floating dollar sign
Uses the floating local currency symbol
Prints a decimal point
Prints a thousand indicator
Using the TO CHAR
-
7/27/2019 day1 session2
40/57
Entry Level Technology Program
Satyam Learning Center ORACLE 40 of 1
SELECT TO_CHAR(SAL, '$99,999.00') SALARY
FROM EMP
WHERE ENAME = 'Ernst';
Using the TO_CHARFunction with Numbers
-
7/27/2019 day1 session2
41/57
Entry Level Technology Program
Satyam Learning Center ORACLE 41 of 1
Using the TO_NUMBERand TO_DATE
Functions
Convert a character string to a number format using theTO_NUMBER function:
Convert a character string to a date format using theTO_DATE function:
These functions have an fx modifier. This modifier specifiesthe exact matching for the character argument and dateformat model of a TO_DATE function
TO_NUMBER(char[, 'format_model'])
TO_DATE(char[, 'format_model'])
Using the TO NUMBER and TO DATE
-
7/27/2019 day1 session2
42/57
Entry Level Technology Program
Satyam Learning Center ORACLE 42 of 1
Using the TO_NUMBERand TO_DATE
Functions
Convert a character string to a number format using theTO_NUMBER function:
Convert a character string to a date format using the TO_DATEfunction:
These functions have anfx modifier. This modifier specifiesthe exact matching for the character argument and date
format model of a TO_DATE function
TO_NUMBER(char[, 'format_model'])
TO_DATE(char[, 'format_model'])
-
7/27/2019 day1 session2
43/57
Entry Level Technology Program
Satyam Learning Center ORACLE 43 of 1
RR Date Format
Current Year1995199520012001
Specified Date27-OCT-9527-OCT-1727-OCT-1727-OCT-95
RR Format1995201720171995
YY Format1995191720172095
If two digits
of thecurrentyear are:
049
049 5099
5099
The return date is in
the current century
The return date is inthe century afterthe current one
The return date is in
the century beforethe current one
The return date is inthe current century
If the specified two-digit year is:
E l f D t F t
-
7/27/2019 day1 session2
44/57
Entry Level Technology Program
Satyam Learning Center ORACLE 44 of 1
Example ofRRDate Format
To find employees hired prior to 1990, use the RR format, whichproduces the same results whether the command is run in 1999or now:
SELECT ENAME, TO_CHAR(HIREDATE, 'DD-Mon-YYYY')
FROM EMP
WHERE HIREDATE < TO_DATE('01-Jan-90', 'DD-Mon-RR');
i i
-
7/27/2019 day1 session2
45/57
Entry Level Technology Program
Satyam Learning Center ORACLE 45 of 1
Nesting Functions
Single-row functions can be nested to anylevel.
Nested functions are evaluated from deepestlevel to the least deep level.
F3(F2(F1(col,arg1),arg2),arg3)
Step 1 = Result 1
Step 2 = Result 2
Step 3 = Result 3
i i
-
7/27/2019 day1 session2
46/57
Entry Level Technology Program
Satyam Learning Center ORACLE 46 of 1
SELECT ENAME,
NVL(TO_CHAR(MGR), 'No Manager')
FROM EMP
WHERE MGR IS NULL;
Nesting Functions
F ti
-
7/27/2019 day1 session2
47/57
Entry Level Technology Program
Satyam Learning Center ORACLE 47 of 1
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_pct,0)
NVL(hire_date,'01-JAN-97')
NVL(job_id,'No Job Yet')
U i th F ti
-
7/27/2019 day1 session2
48/57
Entry Level Technology Program
Satyam Learning Center ORACLE 48 of 1
SELECT ENAME, SAL, NVL(COMM, 0),
(SAL*12) + (SAL*12*NVL(COMM, 0)) AN_SAL
FROM EMP;
Using theNVL Function
C diti l E i
-
7/27/2019 day1 session2
49/57
Entry Level Technology Program
Satyam Learning Center ORACLE 49 of 1
Conditional Expressions
Provide the use of IF-THEN-ELSE logic within aSQL statement
Use two methods:DECODE function
CASE expression
The DECODE F nction
-
7/27/2019 day1 session2
50/57
Entry Level Technology Program
Satyam Learning Center ORACLE 50 of 1
The DECODE Function
Facilitates conditional inquiries by doing the work of aCASE or IF-THEN-ELSE statement:
DECODE(col|expression, search1, result1
[, search2, result2,...,][, default])
Using the DECODE Function
-
7/27/2019 day1 session2
51/57
Entry Level Technology Program
Satyam Learning Center ORACLE 51 of 1
Using the DECODE Function
SELECT ENAME, JOB, SAL,
DECODE (JOB, MANAGER', 1.10*SAL,
'CLERK', 1.15*SAL,
SALESMAN', 1.20*SAL,
SAL)
REVISED_SALARY
FROM EMP;
Using the DECODE Function
-
7/27/2019 day1 session2
52/57
Entry Level Technology Program
Satyam Learning Center ORACLE 52 of 1
Using the DECODE Function
SELECT ENAME, SAL,DECODE (TRUNC(SAL/2000, 0),
0, 0.00,1, 0.09,2, 0.20,3, 0.30,4, 0.40,5, 0.42,
6, 0.44,0.45) TAX_RATE
FROM EMPWHERE DEPTNO = 10;
Display the applicable tax rate for each employee indepartment 80.
The CASE Expression
-
7/27/2019 day1 session2
53/57
Entry Level Technology Program
Satyam Learning Center ORACLE 53 of 1
The CASE Expression
Facilitates conditional inquiries by doing the work ofan IF-THEN-ELSE statement:
CASE exprWHEN comparison_expr1 THEN return_expr1
[WHEN comparison_expr2THEN return_expr2WHEN comparison_exprn THEN return_exprnELSE else_expr]
END
Using the CASE Expression
-
7/27/2019 day1 session2
54/57
Entry Level Technology Program
Satyam Learning Center ORACLE 54 of 1
SELECT ENAME, JOB, SAL,
CASE JOB WHEN MANAGER' THEN 1.10*SAL
WHEN 'CLERK' THEN 1.15*SAL
WHEN 'SALESMAN' THEN 1.20*SAL
ELSE SAL END "REVISED_SALARY"
FROM EMP;
Using the CASE Expression
Facilitates conditional inquiries by doing the work of an IF-
THEN-ELSE statement:
Mi ll F ti
-
7/27/2019 day1 session2
55/57
Entry Level Technology Program
Satyam Learning Center ORACLE 55 of 1
Miscellaneous Functions
REPLACE() TRANSLATE() FLOOR() CEIL()
UPPER() LOWER()
INSTR() INITCAP()
LENGTH() ASCII()
POWER() REVERSE()
E i
-
7/27/2019 day1 session2
56/57
Entry Level Technology Program
Satyam Learning Center ORACLE 56 of 1
Exercise
1) Display only stars (*) depends on no ofthousands in a number (Ex. 5678 , print only5 stars)
2) Add Mr. or Ms. before the name based
on their sex 3)Give the promotion to the employees
based on their designations
4) Give the increments to the employees
based on their designation.
Summary
-
7/27/2019 day1 session2
57/57
Entry Level Technology Program
Summary
In this lesson, you should have learned how to:
Perform calculations on data using functions
Modify individual data items using functions
Manipulate output for groups of rows using functions
Alter date formats for display using functions
Convert column data types using functions
Use NVL functions
Use IF-THEN-ELSE logic
top related