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

NUMTODSINTERVAL

SELECT e.last_name, NULLIF(e.job_id, j.job_id) "Old Job ID"

FROM employees e, job_history j

WHERE e.employee_id = j.employee_id

ORDER BY last_name;

LAST_NAME

Old Job ID

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

----------

De Haan

AD_VP

Hartstein

MK_MAN

Kaufling

ST_MAN

Kochhar

AD_VP

Kochhar

AD_VP

Raphaely

PU_MAN

Taylor

SA_REP

Taylor

 

Whalen

AD_ASST

Whalen

 

NUMTODSINTERVAL

Syntax numtodsinterval::=

NUMTODSINTERVAL ( n , char_expr )

Purpose

NUMTODSINTERVAL converts n to an INTERVAL DAY TO SECOND literal. n can be a number or an expression resolving to a number. char_expr can be of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype. The value for char_expr specifies the unit of n and must resolve to one of the following string values:

DAY

HOUR

MINUTE

SECOND

char_expr is case insensitive. Leading and trailing values within the parentheses are ignored. By default, precision of the return is 9.

Functions 6-111

NUMTOYMINTERVAL

Examples

The following example calculates, for each employee, the number of employees hired by the same manager within the last 100 days from his/her hire date:

SELECT manager_id, last_name, hire_date,

COUNT(*) OVER (PARTITION BY manager_id ORDER BY hire_date

RANGE NUMTODSINTERVAL(100, ’day’) PRECEDING) AS t_count

FROM employees;

MANAGER_ID

LAST_NAME

HIRE_DATE

T_COUNT

----------

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

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

 

100

Kochhar

21-SEP-89

1

 

100

De Haan

13-JAN-93

1

 

100

Raphaely

07-DEC-94

1

 

100

Kaufling

01-MAY-95

1

 

100

Hartstein

17-FEB-96

1

.

 

 

 

 

.

 

 

 

 

.

149

Grant

24-MAY-99

1

 

 

149

Johnson

04-JAN-00

1

 

201

Goyal

17-AUG-97

1

 

205

Gietz

07-JUN-94

1

 

 

King

17-JUN-87

1

NUMTOYMINTERVAL

Syntax numtoyminterval::=

NUMTOYMINTERVAL ( n , char_expr )

Purpose

NUMTOYMINTERVAL converts number n to an INTERVAL YEAR TO MONTH literal. n can be a number or an expression resolving to a number. char_expr can be of

CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype. The value for char_expr specifies the unit of n, and must resolve to one of the following string values:

YEAR

MONTH

char_expr is case insensitive. Leading and trailing values within the parentheses are ignored. By default, precision of the return is 9.

6-112 Oracle9i SQL Reference

NVL

Examples

The following example calculates, for each employee, the total salary of employees hired in the past one year from his/her hire date.

SELECT last_name, hire_date, salary, SUM(salary)

OVER (ORDER BY hire_date

RANGE NUMTOYMINTERVAL(1,’year’) PRECEDING) AS t_sal

FROM employees;

LAST_NAME

HIRE_DATE

SALARY

T_SAL

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

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

King

17-JUN-87

24000

24000

Whalen

17-SEP-87

4400

28400

Kochhar

21-SEP-89

17000

17000

.

 

 

 

.

 

 

 

.

08-MAR-00

2200

112400

Markle

Ande

24-MAR-00

6400

106500

Banda

21-APR-00

6200

109400

Kumar

21-APR-00

6100

109400

NVL

Syntax nvl::=

NVL ( expr1 , expr2 )

Purpose

NVL lets you replace a null (blank) with a string in the results of a query. If expr1 is null, then NVL returns expr2. If expr1 is not null, then NVL returns expr1. The arguments expr1 and expr2 can have any datatype. If their datatypes are different, then Oracle converts expr2 to the datatype of expr1 before comparing them.

The datatype of the return value is always the same as the datatype of expr1, unless expr1 is character data, in which case the return value’s datatype is VARCHAR2 and is in the character set of expr1.

Functions 6-113

NVL2

Examples

The following example returns a list of employee names and commissions, substituting "Not Applicable" if the employee receives no commission:

SELECT last_name, NVL(TO_CHAR(commission_pct), ’Not Applicable’) "COMMISSION" FROM employees

WHERE last_name LIKE ’B%’ ORDER BY last_name;

LAST_NAME

COMMISSION

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

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

Baer

Not Applicable

Baida

Not Applicable

Banda

.1

Bates

.15

Bell

Not Applicable

Bernstein

.25

Bissot

Not Applicable

Bloom

.2

Bull

Not Applicable

NVL2

Syntax nvl2::=

NVL2 ( expr1 , expr2 , expr3 )

Purpose

NVL2 lets you determine the value returned by a query based on whether a specified expression is null or not null. If expr1 is not null, then NVL2 returns expr2. If expr1 is null, then NVL2 returns expr3. The argument expr1 can have any datatype. The arguments expr2 and expr3 can have any datatypes except

LONG.

If the datatypes of expr2 and expr3 are different, then Oracle converts expr3 to the datatype of expr2 before comparing them unless expr3 is a null constant. In that case, a datatype conversion is not necessary.

The datatype of the return value is always the same as the datatype of expr2, unless expr2 is character data, in which case the return value’s datatype is

VARCHAR2.

6-114 Oracle9i SQL Reference

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