Professional Documents
Culture Documents
FUNCTIONS
ORACLE
1 of 1
SQL Functions
Entry Level Technology Program
Input arg 1
Output
arg 2
Result value
arg n
ORACLE
2 of 1
Functions
Single-row functions
Multiple-row functions
ORACLE
3 of 1
ORACLE
4 of 1
Group functions operate on sets of rows to give one result per group.
EMPLOYEES
ORACLE
6 of 1
ORACLE
7 of 1
SELECT AVG(SAL), MAX(SAL), MIN(SAL), SUM(SAL) FROM WHERE EMP JOB LIKE '%MAN%';
ORACLE
8 of 1
You can use MIN and MAX for any data type.
SELECT MIN(HIREDATE), MAX(HIREDATE) FROM employees;
WHERE
DEPTNO = 10;
ORACLE 9 of 1
COUNT(DISTINCT expr) returns the number of distinct non-null values of the expr. Display the number of distinct department values in the EMPLOYEES table.
ORACLE
10 of 1
ORACLE
11 of 1
Single-Row Functions
Entry Level Technology Program
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 an expression
function_name [(arg1, arg2,...)]
ORACLE
12 of 1
Single-Row Functions
Entry Level Technology Program
Character
General
Single-row functions
Number
Conversion
Date
ORACLE
13 of 1
Character Functions
Entry Level Technology Program
Character functions
Case-manipulation functions
LOWER UPPER INITCAP
Character-manipulation functions
CONCAT SUBSTR LENGTH INSTR LPAD | RPAD TRIM REPLACE
ORACLE
14 of 1
Function
Result
LOWER('SQL Course')
UPPER('SQL Course')
sql course
SQL COURSE
ORACLE
15 of 1
Display the employee number, name, and department number for employee Higgins:
SELECT EMPNO, ENAME, DEPTNO FROM EMP WHERE ENAME = smith'; no rows selected SELECT EMPNO, ENAME, DEPTNO FROM EMP WHERE LOWER(ENAME) = smith';
ORACLE
16 of 1
Character-Manipulation Functions
Entry Level Technology Program
LENGTH('HelloWorld')
INSTR('HelloWorld', 'W') LPAD(salary,10,'*') RPAD(salary, 10, '*') TRIM('H' FROM 'HelloWorld')
10
6 *****24000 24000***** elloWorld
ORACLE
17 of 1
SELECT EMPNO, CONCAT(ENAME,JOB) NAME, JOB, LENGTH (ENAME), INSTR(ENAME, 'a') "Contains 'a'?" FROM EMP
WHERE
SUBSTR(JOB, 6) = MAN';
ORACLE
18 of 1
Number Functions
Entry Level Technology Program
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
ORACLE
19 of 1
DUAL is a dummy table you can use to view results from functions and calculations.
ORACLE
20 of 1
SELECT
FROM
DUAL;
ORACLE
21 of 1
Calculate the remainder of a salary after it is divided by 5000 for all employees whose job title is sales representative.
SELECT ENAME, SAL, MOD(SAL, 5000) FROM WHERE EMP JOB = 'SALESMAN';
ORACLE
22 of 1
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 20th century by specifying only the last two digits of the year. Allows you to store 20th century dates in the 21st century in the same way.
WHERE
Satyam Learning Center
Date Time
ORACLE
24 of 1
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.
ORACLE
25 of 1
ORACLE
26 of 1
Date Functions
Entry Level Technology Program
Description Number of months between two dates Add calendar months to date Next day of the date specified Last day of the month Round date Truncate date
ORACLE
27 of 1
ORACLE
28 of 1
01-AUG-95
01-JAN-96 01-JUL-95
TRUNC(SYSDATE ,'YEAR')
01-JAN-95
ORACLE
29 of 1
Conversion Functions
Entry Level Technology Program
ORACLE
30 of 1
For assignments, the Oracle server can automatically convert the following:
From To
VARCHAR2 or CHAR
VARCHAR2 or CHAR
NUMBER
DATE
NUMBER
DATE
VARCHAR2
VARCHAR2
ORACLE
31 of 1
For expression evaluation, the Oracle Server can automatically convert the following:
From To
VARCHAR2 or CHAR
VARCHAR2 or CHAR
NUMBER
DATE
ORACLE
32 of 1
NUMBER
CHARACTER
DATE
TO_CHAR
TO_CHAR
ORACLE
33 of 1
NUMBER
CHARACTER
DATE
TO_CHAR
TO_CHAR
ORACLE
34 of 1
TO_CHAR(date, 'format_model')
The format model: Must be enclosed in single quotation marks and is case sensitive 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
ORACLE
35 of 1
Three-letter abbreviation of the month Three-letter abbreviation of the day of the week Full name of the day of the week
Numeric day of the month
ORACLE 36 of 1
HH24:MI:SS AM
15:45:32 PM
DD "of" MONTH
12 of OCTOBER
ddspth
fourteenth
ORACLE
37 of 1
FROM
EMP;
ORACLE
38 of 1
TO_CHAR(number, 'format_model')
These are some of the format elements you can use with the TO_CHAR function to display a number value as a character:
9 0 $ L . ,
Satyam Learning Center
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
ORACLE 39 of 1
ORACLE
40 of 1
Functions
TO_NUMBER(char[, 'format_model'])
Convert a character string to a date format using the TO_DATE function:
TO_DATE(char[, 'format_model'])
These functions have an fx modifier. This modifier specifies the exact matching for the character argument and date format model of a TO_DATE function
ORACLE
41 of 1
Convert a character string to a number format using the TO_NUMBER function: TO_NUMBER(char[, 'format_model']) Convert a character string to a date format using the TO_DATE function:
TO_DATE(char[, 'format_model'])
These functions have an fx modifier. This modifier specifies the exact matching for the character argument and date format model of a TO_DATE function
ORACLE
42 of 1
RR Date Format
Entry Level Technology Program
5099 The return date is in the century before the current one The return date is in the current century
5099
43 of 1
To find employees hired prior to 1990, use the RR format, which produces the same results whether the command is run in 1999 or now:
ORACLE
44 of 1
Nesting Functions
Entry Level Technology Program
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
ORACLE
45 of 1
Nesting Functions
Entry Level Technology Program
SELECT ENAME, NVL(TO_CHAR(MGR), 'No Manager') FROM WHERE EMP MGR IS NULL;
ORACLE
46 of 1
NVL Function
Entry Level Technology Program
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')
ORACLE
47 of 1
SELECT ENAME, SAL, NVL(COMM, 0), (SAL*12) + (SAL*12*NVL(COMM, 0)) AN_SAL FROM EMP;
ORACLE
48 of 1
Conditional Expressions
Entry Level Technology Program
Provide the use of IF-THEN-ELSE logic within a SQL statement Use two methods: DECODE function CASE expression
ORACLE
49 of 1
ORACLE
50 of 1
'CLERK', 1.15*SAL,
SALESMAN',
SAL) REVISED_SALARY FROM EMP;
1.20*SAL,
ORACLE
51 of 1
Display the applicable tax rate for each employee in department 80.
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 EMP WHERE DEPTNO = 10;
ORACLE
52 of 1
ORACLE
53 of 1
SELECT ENAME, JOB, SAL, CASE JOB WHEN MANAGER' THEN 1.10*SAL 1.15*SAL
WHEN 'SALESMAN'
ELSE FROM EMP; SAL END
THEN
1.20*SAL
"REVISED_SALARY"
ORACLE
54 of 1
Miscellaneous Functions
Entry Level Technology Program
ORACLE
55 of 1
Exercise
Entry Level Technology Program
1) Display only stars (*) depends on no of thousands in a number (Ex. 5678 , print only 5 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.
ORACLE
56 of 1
Summary
Entry Level Technology Program
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
ORACLE
57 of 1