Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Semestr2 / 1 - Oracle / Oracle selected docs / SQL reference.pdf
Скачиваний:
24
Добавлен:
12.05.2015
Размер:
11.92 Mб
Скачать

LOWER

INSERT INTO local_test VALUES

(TO_TIMESTAMP(LOCALTIMESTAMP, ’DD-MON-RR HH.MI.SSXFF’));

The following statement uses the correct format mask to match the return type of

LOCALTIMESTAMP:

INSERT INTO local_test VALUES

(TO_TIMESTAMP(LOCALTIMESTAMP, ’DD-MON-RR HH.MI.SSXFF PM’));

LOG

Syntax log::=

LOG ( m , n )

Purpose

LOG returns the logarithm, base m, of n. The base m can be any positive number other than 0 or 1 and n can be any positive number.

Examples

The following example returns the log of 100:

SELECT LOG(10,100) "Log base 10 of 100" FROM DUAL;

Log base 10 of 100

------------------

2

LOWER

Syntax lower::=

LOWER ( char )

Functions 6-91

LPAD

Purpose

LOWER returns char, with all letters lowercase. char can be any of the datatypes

CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The return value is the same datatype as char.

Examples

The following example returns a string in lowercase:

SELECT LOWER(’MR. SCOTT MCMILLAN’) "Lowercase"

FROM DUAL;

Lowercase

--------------------

mr. scott mcmillan

LPAD

Syntax lpad::=

 

 

 

 

,

char2

LPAD

(

char1

,

n

)

Purpose

LPAD returns char1, left-padded to length n with the sequence of characters in char2; char2 defaults to a single blank. If char1 is longer than n, then this function returns the portion of char1 that fits in n.

Both char1 and char2 can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The string returned is of VARCHAR2 datatype and is in the same character set as char1.

The argument n is the total length of the return value as it is displayed on your terminal screen. In most character sets, this is also the number of characters in the return value. However, in some multibyte character sets, the display length of a character string can differ from the number of characters in the string.

Examples

The following example left-pads a string with the characters "*" and ".":

6-92 Oracle9i SQL Reference

LTRIM

SELECT LPAD(’Page 1’,15,’*.’) "LPAD example"

FROM DUAL;

LPAD example

---------------

*.*.*.*.*Page 1

LTRIM

Syntax ltrim::=

 

 

,

set

LTRIM

(

char

)

Purpose

LTRIM removes characters from the left of char, with all the leftmost characters that appear in set removed; set defaults to a single blank. If char is a character literal, then you must enclose it in single quotes. Oracle begins scanning char from its first character and removes all characters that appear in set until reaching a character not in set and then returns the result.

Both char and set can be any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB. The string returned is of VARCHAR2 datatype and is in the same character set as char.

Examples

The following example trims all of the left-most x’s and y’s from a string:

SELECT LTRIM(’xyxXxyLAST WORD’,’xy’) "LTRIM example"

FROM DUAL;

LTRIM example

------------

XxyLAST WORD

Functions 6-93

MAKE_REF

MAKE_REF

Syntax make_ref::=

 

 

 

,

 

 

 

table

 

 

MAKE_REF

(

,

key

)

 

 

view

 

 

Purpose

MAKE_REF creates a REF to a row of an object view or a row in an object table whose object identifier is primary key based.

See Also:

Oracle9i Application Developer’s Guide - Fundamentals for more information about object views

DEREF on page 6-58

Examples

The sample schema oe contains an object view oc_inventories based on inventory_typ. The object identifier is product_id. The following example creates a REF to the row in the oc_inventories object view with a product_id of 3003:

SELECT MAKE_REF (oc_inventories, 3003) FROM DUAL;

MAKE_REF(OC_INVENTORIES,3003)

------------------------------------------------------------------

00004A038A0046857C14617141109EE03408002082543600000014260100010001

00290090606002A00078401FE0000000B03C21F040000000000000000000000000

0000000000

6-94 Oracle9i SQL Reference

MAX

MAX

Syntax max::=

 

DISTINCT

 

 

 

 

 

 

ALL

 

OVER

(

analytic_clause

)

MAX

(

expr

)

 

 

 

See Also: "Analytic Functions" on page 6-10 for information on syntax, semantics, and restrictions

Purpose

MAX returns maximum value of expr. You can use it as an aggregate or analytic function.

If you specify DISTINCT, then you can specify only the query_partition_ clause of the analytic_clause. The order_by_clause and windowing_ clause are not allowed.

See Also:

"Aggregate Functions" on page 6-8

"About SQL Expressions" on page 4-2 for information on valid forms of expr

Aggregate Example

The following example determines the highest salary in the hr.employees table:

SELECT MAX(salary) "Maximum" FROM employees;

Maximum

----------

24000

Analytic Example

The following example calculates, for each employee, the highest salary of the employees reporting to the same manager as the employee.

Functions 6-95

MAX

SELECT manager_id, last_name, salary,

MAX(salary) OVER (PARTITION BY manager_id) AS mgr_max FROM employees;

MANAGER_ID

LAST_NAME

SALARY

MGR_MAX

----------

------------------------- ---------- ----------

100

Kochhar

17000

17000

100

De Haan

17000

17000

100

Raphaely

11000

17000

100

Kaufling

7900

17000

100

Fripp

8200

17000

100

Weiss

8000

17000

...

 

 

 

If you enclose this query in the parent query with a predicate, then you can determine the employee who makes the highest salary in each department:

SELECT manager_id, last_name, salary

FROM (SELECT manager_id, last_name, salary,

MAX(salary) OVER (PARTITION BY manager_id) AS rmax_sal FROM employees) WHERE salary = rmax_sal;

MANAGER_ID

LAST_NAME

SALARY

----------

------------------------- ----------

100

Kochhar

17000

100

De Haan

17000

101

Greenberg

12000

101

Higgens

12000

102

Hunold

9000

103

Ernst

6000

108

Faviet

9000

114

Khoo

3100

120

Nayer

3200

120

Taylor

3200

121

Sarchand

4200

122

Chung

3800

123

Bell

4000

124

Rajs

3500

145

Tucker

10000

146

King

10000

147

Vishney

10500

148

Ozer

11500

149

Abel

11000

201

Goyal

6000

205

Gietz

8300

 

King

24000

6-96 Oracle9i SQL Reference

MIN

MIN

Syntax min::=

 

DISTINCT

 

 

 

 

 

 

ALL

 

OVER

(

analytic_clause

)

MIN

(

expr

)

 

 

 

See Also: "Analytic Functions" on page 6-10 for information on syntax, semantics, and restrictions

Purpose

MIN returns minimum value of expr. You can use it as an aggregate or analytic function.

If you specify DISTINCT, then you can specify only the query_partition_ clause of the analytic_clause. The order_by_clause and windowing_ clause are not allowed.

See Also:

"Aggregate Functions" on page 6-8

"About SQL Expressions" on page 4-2 for information on valid forms of expr

Aggregate Example

The following statement returns the earliest hire date in the hr.employees table:

SELECT MIN(hire_date) "Earliest" FROM employees;

Earliest

---------

17-JUN-87

Analytic Example

The following example determines, for each employee, the employees who were hired on or before the same date as the employee. It then determines the subset of employees reporting to the same manager as the employee, and returns the lowest salary in that subset.

Functions 6-97

MOD

SELECT manager_id, last_name, hire_date, salary,

MIN(salary) OVER(PARTITION BY manager_id ORDER BY hire_date

RANGE UNBOUNDED PRECEDING) as p_cmin

FROM employees;

MANAGER_ID

LAST_NAME

HIRE_DATE

SALARY

P_CMIN

----------

-------------------------

--------- ---------- ----------

100

Kochhar

21-SEP-89

17000

17000

100

De Haan

13-JAN-93

17000

17000

100

Raphaely

07-DEC-94

11000

11000

100

Kaufling

01-MAY-95

7900

7900

100

Hartstein

17-FEB-96

13000

7900

100

Weiss

18-JUL-96

8000

7900

100

Russell

01-OCT-96

14000

7900

100

Partners

05-JAN-97

13500

7900

100

Errazuriz

10-MAR-97

12000

7900

.

 

 

 

 

.

.

MOD

Syntax mod::=

MOD ( m , n )

Purpose

MOD returns the remainder of m divided by n. Returns m if n is 0.

Examples

The following example returns the remainder of 11 divided by 4:

SELECT MOD(11,4) "Modulus" FROM DUAL;

Modulus

----------

3

This function behaves differently from the classical mathematical modulus function when m is negative. The classical modulus can be expressed using the MOD function with this formula:

6-98 Oracle9i SQL Reference

Соседние файлы в папке Oracle selected docs