Single-Row Functions: Oracle Corporation, 1998. All Rights Reserved
Single-Row Functions: Oracle Corporation, 1998. All Rights Reserved
Single-Row Functions
Objectives
At the end of this lesson, you should be able to:
Describe various types of functions available in SQL Use character, number, and date functions in SELECT statements
SQL Functions
Input arg 1 Output
arg 2
Result value
arg n
2-3
Functions
Single-row functions
Multiple-row functions
2-4
Single-Row Functions
Manipulate data items Accept arguments and return one value
2-5
Single-Row Functions
Character
General
Single-row functions
Number
Conversion
Date
2-6
Character Functions
Character functions
UPPER INITCAP
2-7
SQL COURSE
Sql Course
2-8
LENGTH('String')
INSTR('String', 'r') LPAD(sal,10,'*')
2-10
2-11
Number Functions
ROUND: Rounds value to specified decimal
45.93 Truncates value to specified decimal 45.92
ROUND(45.926, 2) TRUNC:
TRUNC(45.926, 2)
MOD:
division MOD(1600, 300)
2-12
Returns remainder of
100
2-13
2-14
ENAME SAL COMM MOD(SAL,COMM) ---------- --------- --------- ------------MARTIN 1250 1400 1250 ALLEN 1600 300 100 TURNER 1500 0 1500 WARD 1250 500 250
Copyright Oracle Corporation, 1998. All rights reserved.
2-15
2-16
2-17
2-18
Date Functions
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
2-19
ADD_MONTHS ('11-JAN-94',6)
'11-JUL-94'
NEXT_DAY ('01-SEP-95','FRIDAY')
'08-SEP-95'
LAST_DAY('01-SEP-95')
2-20
Copyright Oracle Corporation, 1998. All rights reserved.
'30-SEP-95'
ROUND('25-JUL-95','MONTH')
01-AUG-95
2-21
Conversion Functions
Datatype conversion
2-22
VARCHAR2 or CHAR
VARCHAR2 or CHAR
NUMBER
DATE
NUMBER
DATE
2-23
VARCHAR2
VARCHAR2
Copyright Oracle Corporation, 1998. All rights reserved.
VARCHAR2 or CHAR
VARCHAR2 or CHAR
NUMBER
DATE
2-24
NUMBER
CHARACTER
DATE
TO_CHAR
TO_CHAR
2-25
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
2-26
Copyright Oracle Corporation, 1998. All rights reserved.
Full year in numbers Year spelled out 2-digit value for month Full name of the month
2-28
RR Date Format
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
50-99
2-29
2-30
Use these formats with the TO_CHAR function to display a number value as a character.
9 0 Represents a number Forces a zero to be displayed
$
L . ,
2-31
SALARY -------$3,000
2-32
2-33
NVL Function
Converts null to an actual value Datatypes that can be used are date, character, and number. Datatypes must match
NVL(comm,0)
NVL(hiredate,'01-JAN-97') NVL(job,'No Job Yet')
2-34
2-35
DECODE Function
Facilitates conditional inquiries by doing the work of a CASE or IF-THEN-ELSE statement
DECODE(col/expression, search1, result1 [, search2, result2,...,] [, default])
2-36
Nesting Functions
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
2-38
Copyright Oracle Corporation, 1998. All rights reserved.
Nesting Functions
SQL> SELECT 2 3 FROM 4 WHERE ename, NVL(TO_CHAR(mgr),'No Manager') emp mgr IS NULL;
2-39
Summary
Use functions to: Perform calculations on data Modify individual data items Manipulate output for groups of rows
2-40
Practice Overview
Creating queries that require the use of numeric, character, and date functions Using concatenation with functions Writing case-insensitive queries to test the usefulness of character functions Performing calculations of years and months of service for an employee Determining the review date for an employee
2-41
Copyright Oracle Corporation, 1998. All rights reserved.
Course Overview
<Enter course-overview information here>
2-42