1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu single row functions pdf

66 298 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Single Row Functions
Chuyên ngành SQL and PL/SQL
Thể loại Giáo trình
Định dạng
Số trang 66
Dung lượng 349,47 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć2... Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć4... Introduction to Oracle: SQL and PL/SQL Using P

Trang 1

Single Row Functions

3

Trang 2

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć2

Trang 3

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

Trang 4

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć4

Trang 5

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 aredifferent 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

Trang 6

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć6

Trang 7

Single Row Functions

Single row functions are used to manipulate data items They accept one or morearguments 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

expression is any character string or calculated expression

arg1, arg2 is any argument to be used by the function

Trang 8

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć8

Trang 9

Character Functions

Single row character functions accept character data as input and can return bothcharacter and number values

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 otherletters in lowercase

CONCAT(column1|expression1,

column2|expression2)

Concatenates the first character value to thesecond character value Equivalent toconcatenation 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 thesecond 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.”

Trang 10

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć10

Trang 11

Character Functions continued

-ladoris ngao Lngao VP, OPERATIONS

midori nagayama Mnagayam VP, SALES

mark quick-to-see Mquickto VP, FINANCE

audry ropeburn Aropebur VP, ADMINISTRATION

Trang 12

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć12

Trang 13

Character Functions continued

Example

Display the first name and last name of all employees with the last name of 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

Trang 14

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć14

Trang 15

Character Functions continued

-Ace Ski Boot 12

Ace Ski Pole 12

Trang 16

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć16

Trang 17

Number Functions

Number functions accept numeric input and return numeric values This sectiondescribes some of the number functions

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 pointare 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.”

Trang 18

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć18

Trang 19

Number Functions continued

The TRUNC and ROUND functions work with similar arguments If the secondargument is 0 or is missing, then the value is truncated or rounded to zero decimalplaces If the second argument is 2, then the value is truncated or rounded to twodecimal places, or to the hundredths Conversely, if the second argument is -2, thenthe 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

Display the value 45.923 truncated to the hundredth, no, and ten decimal places

Trang 20

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć20

Trang 21

Number Functions continued

Trang 22

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć22

Trang 23

Oracle Date Format

Oracle Date Storage

Oracle stores dates in an internal numeric format, representing the following:

DUAL

The DUAL table is owned by the user SYS and may be accessed by all users Itcontains one column, DUMMY, and one row with the value “X.” The DUAL table isuseful 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 userdata

Example

Display the current date using the DUAL table

Trang 24

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć24

Trang 25

Using Arithmetic Operators with Dates

Since the database stores dates as numbers, you can perform calculations usingarithmetic operators such as addition and subtraction You can add and subtractnumber 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

Note: SYSDATE is a SQL function that returns the current date and time Your

results may differ from the examples

Trang 26

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć26

Trang 27

Date Functions

Date functions operate on Oracle dates All date functions return a value of DATEdatatype except MONTHS_BETWEEN, which returns a numeric value

MONTHS_BETWEEN(date1, date2) Finds the number of months between

date1 and date2 The result can be

positive or negative If date1 is later than

date2, the result is positive; if date1 is

earlier than date2, the result is negative.

The non-integer part of the resultrepresents a portion of the month

ADD_MONTHS(date,n) Adds n number of calender months to

date n must be an integer and can be

negative

NEXT_DAY(date,‘char’) Finds the date of the next specified day

of the week (‘char’) following date.

char may be a number representing a

day or a character string

LAST_DAY(date) Finds the date of the last day of the

month that contains date.

ROUND(date[,‘fmt’]) Finds the date of first day of the month

contained in date when no format model

fmt is specified If fmt = YEAR, finds

first day of year containing date This is

useful when comparing dates that mayhave different times

TRUNC(date[,‘fmt’]) Returns date with the time set to

midnight if no format model fmt is

specified This function is useful whenyou want to remove the time portion ofthe date

Note: This list is a subset of the available date functions The format models are

covered later in this chapter Examples of format models are month or year.For more information, see

Oracle7 Server SQL Reference, Release 7.3, “Date Functions.”

Trang 28

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć28

Trang 29

Date Functions continued

Example

For all employees employed less than 48 months, display the employee number, startdate, number of months employed, and the 6 month review date

ID START_DAT TENURE REVIEW

Trang 30

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć30

Trang 31

Date Functions continued

The ROUND and TRUNC functions can be used for number and date values Whenusing these functions with dates, they round or truncate to the specified format model.Therefore, you can round dates to the nearest year or month

ID START_DAT ROUND(STA TRUNC(STA

-

3 17-JUN-91 01-JUL-91 01-JUN-91

6 18-JAN-91 01-FEB-91 01-JAN-91

10 27-FEB-91 01-MAR-91 01-FEB-91

13 18-FEB-91 01-MAR-91 01-FEB-91

15 09-OCT-91 01-OCT-91 01-OCT-91

18 09-FEB-91 01-FEB-91 01-FEB-91

19 06-AUG-91 01-AUG-91 01-AUG-91

20 21-JUL-91 01-AUG-91 01-JUL-91

21 26-MAY-91 01-JUN-91 01-MAY-91

24 17-MAR-91 01-APR-91 01-MAR-91

25 09-MAY-91 01-MAY-91 01-MAY-91

11 rows selected

Trang 32

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć32

Trang 33

Conversion Functions

SQL provides three functions to convert a value from one datatype to another

TO_CHAR(number|date,[‘fmt’]) Converts a number or date value to a

VARCHAR2 character string with

format model fmt.

TO_NUMBER(char) Converts a character string containing

digits to a number

TO_DATE(char,[‘fmt’]) Converts a character string representing

a date to a date value according to the

fmt specified If fmt is omitted, format is

DD-MON-YY

Note: This list is a subset of the available conversion functions.

For more information, see

Oracle7 Server SQL Reference, Release 7.3, “Conversion Functions.”

Trang 34

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć34

Trang 35

TO_CHAR Function with Date Formats

Displaying a Date in a Specific Format

Previously, all Oracle date values were displayed in the DD-MON-YY format TheTO_CHAR function allows you to convert a date from this default format to onespecified by you

D You can resize the display width of the resulting character field with the

SQL*Plus COLUMN command

D The resultant column width is 80 characters by default

Example

Display the order number and date ordered for all orders taken by sales representative

11 Format the date to display the date as 08/92

Trang 36

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć36

Trang 37

TO_CHAR Function with Date Formats continued

Sample Valid Date Format Elements

Years in dates YYYY or SYYYY Year; S prefixes BC date with -

YYY or YY or Y Last 3, 2, or 1 digit(s) of year

IYYY, IYY, IY, I 4, 3, 2, or 1 digit year based on the ISO

length of 9 characters

DDD or DD or D Day of year, month, or week

of 9 characters

December 4713 BC

Trang 38

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć38

Trang 39

TO_CHAR Function with Date Formats continued

A.M or P.M Meridian indicator with periods

HH or HH12 or HH24 Hour of day or hour (1-12) or hour (0-23)

/ , Punctuation is reproduced in the result

“ of the ” Quoted string is reproduced in the result

Using the FM Prefix

The FM prefix suppresses blank padding in month and day names, leaving a

variable-length result It also suppresses leading zeros in numbers A second

occurrence of FM turns blank padding on again

Specifying Suffixes to Influence Number Display

SP Spelled-out number (for example, DDSP for FOUR).SPTH or THSP Spelled-out ordinal numbers (for example, DDSPTH

for FOURTH)

Trang 40

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć40

Trang 41

TO_CHAR Function with Date Formats continued

The RR Date Format Element

The RR date format is similar to the YY element, but it allows you to specify

different centuries You can use the RR date format element instead of YY, so that thecentury of the return value varies according to the specified two-digit year and thelast two digits of the current year The table on the opposite page summarizes thebehavior of the RR element

Current Year Given Date Interpreted (RR) Interpreted (YY)

Trang 42

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć42

Trang 43

TO_CHAR Function with Date Formats continued

Example

Display the names and hire dates for all employees hired in 1991 The hire dateshould look like 7 of February 1991

-Nagayama Seventeenth of June 1991 12:00:00 AM

Urguhart Eighteenth of January 1991 12:00:00 AMHavel Twenty-Seventh of February 1991 12:00:00

Trang 44

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć44

Trang 45

TO_CHAR Function with Number Formats

When working with number values such as character strings, you should convertthose numbers to the character datatype using the TO_CHAR function, which

translates a value of NUMBER datatype to VARCHAR2 datatype This technique isespecially useful with concatenation

Number Format Elements

If you are converting a character datatype that converts digits into a number, you canuse the elements listed below

9 Numeric position (number of 9s

determine display width)

L Floating local currency symbol L999999 FF1234

Decimal point in position specified 999999.99 1234.00

MI Minus signs to right (negative values) 999999MI

1234-PR Parenthesize negative numbers 999999PR <1234>

EEEE Scientific notation (format must

specify four Es)

Trang 46

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć46

Trang 47

TO_CHAR Function with Number Formats continued

Example

Display a message indicating that the order, represented by its number, was filled forall orders shipped on September 21, 1992 Be sure to include the total for the order

-Order 110 was filled for a total of $1,539

Order 111 was filled for a total of $2,770

Guidelines

D The Oracle7 Server displays a string of pound signs (#) in place of a whole

number whose digits exceed the number of digits provided in the format model

D The Oracle7 Server rounds the stored decimal value to the number of decimalspaces provided in the format model

Trang 48

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 3Ć48

Trang 49

TO_NUMBER and TO_DATE Functions

You may want to convert a character string to either a number or a date format Toaccomplish this task, you use the TO_NUMBER or TO_DATE functions,

respectively The format model you choose will be based on the previously

demonstrated format elements

Ngày đăng: 10/12/2013, 17:15

TỪ KHÓA LIÊN QUAN

w