Tải bản đầy đủ (.pdf) (66 trang)

Tài liệu Single row funtions docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (385.82 KB, 66 trang )

Single Row Functions
3
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć2
Schedule: Timing Topic
55 minutes Lecture
40 minutes Practice
95 minutes Total
Class Management Note:
Files required for lesson are:
Demonstration: l3hire.sql
Practice: None
Single Row Functions 3Ć3
Objectives
Functions make the basic query block more powerful and are used to
manipulate data values. This is the first of two lessons that explore functions.
You will focus on single row character, number, and date functions, as well as
those functions that convert data from one type to another, for example,
character data to numeric.
At the end of this lesson, you should be able to
D
Explain the various types of functions available in SQL.
D
Identify the basic concepts of using functions.
D
Use a variety of character, number, and date functions in SELECT statements.
D
Explain the conversion functions and how they might be used.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć4
Single Row Functions 3Ć5
Overview
Functions are a very powerful feature of SQL and can be used to


D
Perform calculations on data.
D
Modify individual data items.
D
Manipulate output for groups of rows.
D
Alter date formats for display.
D
Convert column datatypes.
There are two distinct types of functions:
D
Single row functions.
D
Multiple row functions.
Single Row Functions
These functions operate on single rows only, and return one result per row. There are
different types of single row functions. We will cover those listed below.
D
Character
D
Number
D
Date
D
Conversion
Multiple Row Functions
These functions manipulate groups of rows to give one result per group of rows.
For more information, see
Oracle7 Server SQL Reference, Release 7.3 for the complete list of available

functions and syntax.
Class Management Note:
This lesson does not discuss all functions in great detail. Present the most
common functions without a long explanation.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć6
Single Row Functions 3Ć7
Single Row Functions
Single row functions are used to manipulate data items. They accept one or more
arguments and return one value for each row returned by the query. An argument may
be one of the following:
D
A user-supplied constant
D
A variable value
D
A column name
D
An expression
Features of Single Row Functions
D
They act on each row returned in the query.
D
They return one result per row.
D
They may return a data value of a different type than that referenced.
D
They may expect one or more user arguments.
D
You can nest them.
D

You can use them in SELECT, WHERE, and ORDER BY clauses.
Syntax
function_name (column|expression, [arg1, arg2,...])
where: function_name is the name of the function.
column is any named database column.
expression is any character string or calculated expression.
arg1, arg2 is any argument to be used by the function.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć8
Single Row Functions 3Ć9
Character Functions
Single row character functions accept character data as input and can return both
character and number values.
Function
Purpose
LOWER(column|expression) Converts alpha character values to lowercase.
UPPER(column|expression) Converts alpha character values to uppercase.
INITCAP(column|expression) Converts alpha character values to uppercase
for the first letter of each word, all other
letters in lowercase.
CONCAT(column1|expression1,
column2|expression2)
Concatenates the first character value to the
second character value. Equivalent to
concatenation operator (||).
SUBSTR(column|expression,m[,n]) Returns specified characters from character
value starting at character position m, n
characters long. If m is negative, the count
starts from the end of the character value.
LENGTH(column|expression) Returns the number of characters in value.
NVL(column|expression1,column|ex

pression2)
Converts the the first value if null to the
second value.
Note: This list is a subset of the available character functions.
For more information, see
Oracle7 Server SQL Reference, Release 7.3, “Character Functions.”
Class Management Note:
The NVL function was covered in Lesson 1 under the topic “Managing Null
Values.”
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć10
Single Row Functions 3Ć11
Character Functions
continued
Example
Display the first and last name in lowercase, userid in initial capitalization, and title in
uppercase for all vice presidents.
SQL> SELECT LOWER(first_name||’ ’||last_name) VP,
2 INITCAP(userid) USERID,
3 UPPER(title) TITLE
4 FROM s_emp
5 WHERE title LIKE ’VP%’;
VP USERID TITLE
--------------------- -------- -------------------
ladoris ngao Lngao VP, OPERATIONS
midori nagayama Mnagayam VP, SALES
mark quick-to-see Mquickto VP, FINANCE
audry ropeburn Aropebur VP, ADMINISTRATION
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć12
Single Row Functions 3Ć13
Character Functions

continued
Example
Display the first name and last name of all employees with the last name of Patel.
SQL> SELECT first_name, last_name
2 FROM s_emp
3 WHERE last_name = ’PATEL’;
no rows returned
SQL> SELECT first_name, last_name
2 FROM s_emp
3 WHERE UPPER(last_name) = ’PATEL’;
FIRST_NAME LAST_NAME
-------------------- --------------------
Vikram Patel
Radha Patel
Note: The name is displayed as it was stored in the database. To display the name in
uppercase, the UPPER function must be used in the SELECT clause as well.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć14
Single Row Functions 3Ć15
Character Functions
continued
Example
Display the name and country of all customers with a good credit rating. Concatenate
name and country.
SQL> SELECT CONCAT (name, country) CUSTOMER
2 FROM s_customer
3 WHERE credit_rating = ’GOOD’;
CUSTOMER
-------------------------------------------------
Delhi SportsIndia
Sweet Rock SportsNigeria

Example
Display the product name and length of name for all products where the first three
characters are Ace.
SQL> SELECT name, LENGTH(name)
2 FROM s_product
3 WHERE SUBSTR(name,1,3) = ’Ace’;
NAME LENGTH(NAME)
-------------------- ------------
Ace Ski Boot 12
Ace Ski Pole 12
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć16
Single Row Functions 3Ć17
Number Functions
Number functions accept numeric input and return numeric values. This section
describes some of the number functions.
Function
Purpose
ROUND(column|expression,n) Rounds the column, expression, or value to n
decimal places. If n is omitted, no decimal
places. If n is negative, numbers to left of the
decimal point are rounded.
TRUNC(column|expression,n) Truncates the column or value to n decimal
places, or if n is omitted, no decimal places. If n
is negative, numbers left of the decimal point
are truncated to zero.
MOD(m,n) Returns the remainder of m divided by n.
Note: This list is a subset of the available number functions.
For more information, see
Oracle7 Server SQL Reference, Release 7.3, “Number Functions.”
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć18

Single Row Functions 3Ć19
Number Functions
continued
The TRUNC and ROUND functions work with similar arguments. If the second
argument is 0 or is missing, then the value is truncated or rounded to zero decimal
places. If the second argument is 2, then the value is truncated or rounded to two
decimal places, or to the hundredths. Conversely, if the second argument is -2, then
the value is truncated or rounded to two decimal places to the left, or to the hundreds.
Note: ROUND and TRUNC may also be used with date functions. You will see
examples later in this lesson.
Example
Display the value 45.923 rounded to the hundredth, no, and ten decimal places.
SQL> SELECT ROUND(45.923,2), ROUND(45.923,0),
2 ROUND(45.923,-1)
3 FROM SYS.DUAL;
ROUND(45.923,2) ROUND(45.923,0) ROUND(45.923,-1)
--------------- --------------- ----------------
45.92 46 50
Example
Display the value 45.923 truncated to the hundredth, no, and ten decimal places.
SQL> SELECT TRUNC(45.923,2), TRUNC(45.923),
2 TRUNC(45.923,-1)
3 FROM SYS.DUAL;
TRUNC(45.923,2) TRUNC(45.923) TRUNC(45.923,-1)
--------------- --------------- ----------------
45.92 45 40
SYS.DUAL is a dummy table. It will be covered in detail later in this lesson.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć20
Single Row Functions 3Ć21
Number Functions

continued
Example
Calculate the remainder of the ratio of salary to commission for all employees whose
salary is more than 1400.
SQL> SELECT last_name, MOD(salary,commission_pct)
2 FROM s_emp
3 WHERE salary > 1400;
LAST_NAME MOD(SALARY,COMMISSION_PCT)
------------ --------------------------
Velasquez
Ngao
Quick-To-See
Ropeburn
Giljum 2.5
Sedeghi 5
Nguyen 10
Dumas 15
8 rows selected.
Class Management Note:
The feedback from SQL*Plus is now showing. This topic is covered in a
later lesson.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć22
Class Management Note:
Stress that the default date displays in the format DD-MON-YY. Functions
are used to display other values, such as hour, minute, second, and century.
Explain that DUAL is a dummy table. Mention that SYS is the owner’s
name, and that this is the most efficient way of referencing DUAL. It is
useful for returning a value when access to a “real” data table is not
required.
Single Row Functions 3Ć23

Oracle Date Format
Oracle Date Storage
Oracle stores dates in an internal numeric format, representing the following:
D
Century
D
Year
D
Month
D
Day
D
Hours
D
Minutes
D
Seconds
The default display and input format for any date is DD-MON-YY. Valid Oracle dates
are between January 1, 4712 B.C. and December 31, 4712 A.D.
SYSDATE
SYSDATE is a date function that returns the current date and time. You can use
SYSDATE just as you would use any other column name. For example, you can
display the current date by selecting SYSDATE from a table. It is customary to select
SYSDATE from a dummy table called DUAL.
DUAL
The DUAL table is owned by the user SYS and may be accessed by all users. It
contains one column, DUMMY, and one row with the value “X.” The DUAL table is
useful when you want to return a value once only, for instance, the value of a
constant, pseudo-column, or expression that is not derived from a table with user
data.

Example
Display the current date using the DUAL table.
SQL> SELECT SYSDATE
2 FROM SYS.DUAL;
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder3Ć24
Single Row Functions 3Ć25
Using Arithmetic Operators with Dates
Since the database stores dates as numbers, you can perform calculations using
arithmetic operators such as addition and subtraction. You can add and subtract
number constants as well as dates.
Arithmetic Operations on Dates
You can perform the following operations:
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 days Subtracts one date from another.
date + number/24 date Adds a number of hours to a date.
Example
For employees in department 43, display the last name and number of weeks
employed.
SQL> SELECT last_name, (SYSDATE-start_date)/7 WEEKS
2 FROM s_emp
3 WHERE dept_id = 43;
LAST_NAME WEEKS
------------ ----------
Biri 297.226498
Markarian 238.083641
Newman 230.083641
Note: SYSDATE is a SQL function that returns the current date and time. Your

results may differ from the examples.
Class Management Note:
If an older date is subtracted from a more current date, the difference is a
negative number.

×