Sei sulla pagina 1di 57

Entry Level Technology Program

FUNCTIONS

Satyam Learning Center

ORACLE

1 of 1

SQL Functions
Entry Level Technology Program

Input arg 1

Function Function performs action

Output

arg 2

Result value

arg n

Satyam Learning Center

ORACLE

2 of 1

Two Types of SQL Functions


Entry Level Technology Program

Functions

Single-row functions

Multiple-row functions

Satyam Learning Center

ORACLE

3 of 1

Entry Level Technology Program

Aggregating Data Using Group Functions

Satyam Learning Center

ORACLE

4 of 1

What Are Group Functions?


Entry Level Technology Program

Group functions operate on sets of rows to give one result per group.
EMPLOYEES

The maximum salary in the EMPLOYEES table.

Satyam Learning Center ORACLE 5 of 1

Types of Group Functions


Entry Level Technology Program

AVG COUNT MAX MIN SUM

Satyam Learning Center

ORACLE

6 of 1

Group Functions Syntax


Entry Level Technology Program

SELECT FROM [WHERE [GROUP BY [ORDER BY

[column,] group_function(column), ... table condition] column] column];

Satyam Learning Center

ORACLE

7 of 1

Entry Level Technology Program

Using the AVG and SUM Functions

You can use AVG and SUM for numeric data.

SELECT AVG(SAL), MAX(SAL), MIN(SAL), SUM(SAL) FROM WHERE EMP JOB LIKE '%MAN%';

Satyam Learning Center

ORACLE

8 of 1

Entry Level Technology Program

Using the MIN ,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;
ORACLE 9 of 1

Satyam Learning Center

Using the DISTINCT Keyword


Entry Level Technology Program

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.

SELECT COUNT(DISTINCT DEPTNO) FROM EMP;

Satyam Learning Center

ORACLE

10 of 1

Entry Level Technology Program

Group Functions and Null Values

Group functions ignore null values in the column.


SELECT AVG(COMM)
FROM EMP;

The NVL function forces group functions to include null values.


SELECT AVG(NVL(COMM, 0)) FROM EMP;

Satyam Learning Center

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,...)]

Satyam Learning Center

ORACLE

12 of 1

Single-Row Functions
Entry Level Technology Program

Character

General

Single-row functions

Number

Conversion

Date

Satyam Learning Center

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

Satyam Learning Center

ORACLE

14 of 1

Case Manipulation Functions


Entry Level Technology Program

These functions convert case for character strings.

Function

Result

LOWER('SQL Course')
UPPER('SQL Course')

sql course
SQL COURSE

INITCAP('SQL Course') Sql Course

Satyam Learning Center

ORACLE

15 of 1

Entry Level Technology Program

Using Case Manipulation Functions

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';

Satyam Learning Center

ORACLE

16 of 1

Character-Manipulation Functions
Entry Level Technology Program

These functions manipulate character strings:


Function CONCAT('Hello', 'World') SUBSTR('HelloWorld',1,5) Result HelloWorld Hello

LENGTH('HelloWorld')
INSTR('HelloWorld', 'W') LPAD(salary,10,'*') RPAD(salary, 10, '*') TRIM('H' FROM 'HelloWorld')

10
6 *****24000 24000***** elloWorld

Satyam Learning Center

ORACLE

17 of 1

Entry Level Technology Program

Using the CharacterManipulation Functions

SELECT EMPNO, CONCAT(ENAME,JOB) NAME, JOB, LENGTH (ENAME), INSTR(ENAME, 'a') "Contains 'a'?" FROM EMP

WHERE

SUBSTR(JOB, 6) = MAN';

Satyam Learning Center

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

Satyam Learning Center

ORACLE

19 of 1

Using the ROUND Function


Entry Level Technology Program

SELECT ROUND(45.923,2), ROUND(45.923,0), ROUND(45.923,-1) FROM DUAL;

DUAL is a dummy table you can use to view results from functions and calculations.

Satyam Learning Center

ORACLE

20 of 1

Using the TRUNC Function


Entry Level Technology Program

SELECT

TRUNC(45.923,2), TRUNC(45.923), TRUNC(45.923,-2)

FROM

DUAL;

Satyam Learning Center

ORACLE

21 of 1

Using the MOD Function


Entry Level Technology Program

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';

Satyam Learning Center

ORACLE

22 of 1

Working with Dates


Entry Level Technology Program

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.

SELECT ENAME, HIREDATE 0 FROM EMP

WHERE
Satyam Learning Center

ENAME like 'G%';


ORACLE 23 of 1

Working with Dates


Entry Level Technology Program

SYSDATE is a function that returns:

Date Time

Satyam Learning Center

ORACLE

24 of 1

Arithmetic with Dates


Entry Level Technology Program

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.

Satyam Learning Center

ORACLE

25 of 1

Entry Level Technology Program

Using Arithmetic Operators with Dates

SELECT ENAME, (SYSDATE-HIREDATE)/7 AS WEEKS FROM WHERE EMP DEPTNO = 20;

Satyam Learning Center

ORACLE

26 of 1

Date Functions
Entry Level Technology Program

Function MONTHS_BETWEEN ADD_MONTHS NEXT_DAY LAST_DAY ROUND TRUNC

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

Satyam Learning Center

ORACLE

27 of 1

Using Date Functions


Entry Level Technology Program

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-FEB-95') '28-FEB-95'

Satyam Learning Center

ORACLE

28 of 1

Using Date Functions


Entry Level Technology Program

Assume SYSDATE = '25-JUL-95': ROUND(SYSDATE,'MONTH')


ROUND(SYSDATE ,'YEAR') TRUNC(SYSDATE ,'MONTH')

01-AUG-95
01-JAN-96 01-JUL-95

TRUNC(SYSDATE ,'YEAR')

01-JAN-95

Satyam Learning Center

ORACLE

29 of 1

Conversion Functions
Entry Level Technology Program

Data type conversion

Implicit data type conversion

Explicit data type conversion

Satyam Learning Center

ORACLE

30 of 1

Entry Level Technology Program

Implicit Data Type Conversion

For assignments, the Oracle server can automatically convert the following:
From To

VARCHAR2 or CHAR
VARCHAR2 or CHAR

NUMBER
DATE

NUMBER
DATE

VARCHAR2
VARCHAR2

Satyam Learning Center

ORACLE

31 of 1

Entry Level Technology Program

Implicit Data Type Conversion

For expression evaluation, the Oracle Server can automatically convert the following:
From To

VARCHAR2 or CHAR
VARCHAR2 or CHAR

NUMBER
DATE

Satyam Learning Center

ORACLE

32 of 1

Entry Level Technology Program

Explicit Data Type Conversion


TO_NUMBER TO_DATE

NUMBER

CHARACTER

DATE

TO_CHAR

TO_CHAR

Satyam Learning Center

ORACLE

33 of 1

Entry Level Technology Program

Explicit Data Type Conversion


TO_NUMBER TO_DATE

NUMBER

CHARACTER

DATE

TO_CHAR

TO_CHAR

Satyam Learning Center

ORACLE

34 of 1

Entry Level Technology Program

Using the TO_CHAR Function with Dates

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

Satyam Learning Center

ORACLE

35 of 1

Entry Level Technology Program

Elements of the Date Format Model


Full year in numbers Year spelled out Two-digit value for month Full name of the month

YYYY YEAR MM MONTH MON DY DAY DD


Satyam Learning Center

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

Entry Level Technology Program

Elements of the Date Format Model

Time elements format the time portion of the date.

HH24:MI:SS AM

15:45:32 PM

Add character strings by enclosing them in double quotation marks.

DD "of" MONTH

12 of OCTOBER

Number suffixes spell out numbers.

ddspth

fourteenth

Satyam Learning Center

ORACLE

37 of 1

Entry Level Technology Program

Using the TO_CHAR Function with Dates

SELECT ENAME, TO_CHAR(HIREDATE, 'fmDD Month YYYY') AS HIREDATE

FROM

EMP;

Satyam Learning Center

ORACLE

38 of 1

Using the TO_CHAR Function with Numbers


Entry Level Technology Program

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

Entry Level Technology Program

Using the TO_CHAR Function with Numbers

SELECT TO_CHAR(SAL, '$99,999.00') SALARY FROM WHERE EMP ENAME = 'Ernst';

Satyam Learning Center

ORACLE

40 of 1

Using the TO_NUMBER and TO_DATE


Entry Level Technology Program

Functions

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

Satyam Learning Center

ORACLE

41 of 1

Using the TO_NUMBER and TO_DATE Functions


Entry Level Technology Program

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

Satyam Learning Center

ORACLE

42 of 1

RR Date Format
Entry Level Technology Program

Current Year 1995 1995 2001 2001

Specified Date 27-OCT-95 27-OCT-17 27-OCT-17 27-OCT-95

RR Format 1995 2017 2017 1995

YY Format 1995 1917 2017 2095

If the specified two-digit year is:


049 If two digits of the current year are: 049 The return date is in the current century The return date is in the century after the current one
ORACLE

5099 The return date is in the century before the current one The return date is in the current century

5099

Satyam Learning Center

43 of 1

Example of RR Date Format


Entry Level Technology Program

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:

SELECT ENAME, TO_CHAR(HIREDATE, 'DD-Mon-YYYY')


FROM EMP

WHERE HIREDATE < TO_DATE('01-Jan-90', 'DD-Mon-RR');

Satyam Learning Center

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

Satyam Learning Center

ORACLE

45 of 1

Nesting Functions
Entry Level Technology Program

SELECT ENAME, NVL(TO_CHAR(MGR), 'No Manager') FROM WHERE EMP MGR IS NULL;

Satyam Learning Center

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')

Satyam Learning Center

ORACLE

47 of 1

Using the NVL Function


Entry Level Technology Program

SELECT ENAME, SAL, NVL(COMM, 0), (SAL*12) + (SAL*12*NVL(COMM, 0)) AN_SAL FROM EMP;

Satyam Learning Center

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

Satyam Learning Center

ORACLE

49 of 1

The DECODE Function


Entry Level Technology Program

Facilitates conditional inquiries by doing the work of a CASE or IF-THEN-ELSE statement:


DECODE(col|expression, search1, result1 [, search2, result2,...,] [, default])

Satyam Learning Center

ORACLE

50 of 1

Using the DECODE Function


Entry Level Technology Program

SELECT ENAME, JOB, SAL, DECODE (JOB, MANAGER', 1.10*SAL,

'CLERK', 1.15*SAL,

SALESMAN',
SAL) REVISED_SALARY FROM EMP;

1.20*SAL,

Satyam Learning Center

ORACLE

51 of 1

Using the DECODE Function


Entry Level Technology Program

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;

Satyam Learning Center

ORACLE

52 of 1

The CASE Expression


Entry Level Technology Program

Facilitates conditional inquiries by doing the work of an IF-THEN-ELSE statement:


CASE expr WHEN [WHEN WHEN ELSE END comparison_expr1 THEN return_expr1 comparison_expr2 THEN return_expr2 comparison_exprn THEN return_exprn else_expr]

Satyam Learning Center

ORACLE

53 of 1

Using the CASE Expression


Entry Level Technology Program

Facilitates conditional inquiries by doing the work of an IFTHEN-ELSE statement:

SELECT ENAME, JOB, SAL, CASE JOB WHEN MANAGER' THEN 1.10*SAL 1.15*SAL

WHEN 'CLERK' THEN

WHEN 'SALESMAN'
ELSE FROM EMP; SAL END

THEN

1.20*SAL

"REVISED_SALARY"

Satyam Learning Center

ORACLE

54 of 1

Miscellaneous Functions
Entry Level Technology Program

REPLACE() FLOOR() UPPER() INSTR() LENGTH() POWER()

TRANSLATE() CEIL() LOWER() INITCAP() ASCII() REVERSE()

Satyam Learning Center

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.

Satyam Learning Center

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

Satyam Learning Center

ORACLE

57 of 1

Potrebbero piacerti anche