0% found this document useful (0 votes)
5 views

Slide 10 - General Functions

This lesson covers implicit and explicit data type conversion, including the use of TO_CHAR, TO_DATE, and TO_NUMBER functions. It also discusses general functions like NVL, NVL2, NULLIF, and COALESCE, as well as conditional expressions such as CASE and DECODE. The lesson emphasizes altering date formats, converting column data types, and applying conditional logic in SQL statements.

Uploaded by

Bulut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Slide 10 - General Functions

This lesson covers implicit and explicit data type conversion, including the use of TO_CHAR, TO_DATE, and TO_NUMBER functions. It also discusses general functions like NVL, NVL2, NULLIF, and COALESCE, as well as conditional expressions such as CASE and DECODE. The lesson emphasizes altering date formats, converting column data types, and applying conditional logic in SQL statements.

Uploaded by

Bulut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lesson Agenda

• Implicit and explicit data type conversion


• TO_CHAR, TO_DATE, TO_NUMBER functions

4
• Nesting functions
Using General Functions and • General functions:
– NVL
Conditional Expressions
– NVL2
– NULLIF
– COALESCE
• Conditional expressions:
– CASE
– DECODE

Copyright © 2009, Oracle. All rights reserved. 4-2 Copyright © 2009, Oracle. All rights reserved.

General Functions NVL Function

The following functions work with any data type and pertain to Converts a null value to an actual value:
using nulls: • Data types that can be used are date, character, and
• NVL (expr1, expr2) number.
• NVL2 (expr1, expr2, expr3) • Data types must match:
• NULLIF (expr1, expr2) – NVL(commission_pct,0)
• COALESCE (expr1, expr2, ..., exprn) – NVL(hire_date,'01-JAN-97')
– NVL(job_id,'No Job Yet')

4-3 Copyright © 2009, Oracle. All rights reserved. 4-4 Copyright © 2009, Oracle. All rights reserved.

Using the NVL Function Using the NVL2 Function


NVL2 checks whether the value is NULL or NOT. If not NULL,
use the first expression, if null, use the second expression.
1
SELECT last_name, salary, NVL(commission_pct, 0),
(salary*12) + (salary*12*NVL(commission_pct, 0)) AN_SAL 2 SELECT last_name, salary, commission_pct, 1
FROM employees; NVL2(commission_pct,
'SAL+COMM', 'SAL') income 2
FROM employees WHERE department_id IN (50, 80);

… 2
1 2 1

4-5 Copyright © 2009, Oracle. All rights reserved. 4-6 Copyright © 2009, Oracle. All rights reserved.
Using the NULLIF Function Using the COALESCE Function

1
• The advantage of the COALESCE function over the NVL
SELECT first_name, LENGTH(first_name) "expr1", function is that the COALESCE function can take multiple
last_name, LENGTH(last_name) "expr2", 2 alternate values.
NULLIF(LENGTH(first_name), LENGTH(last_name)) result 3 • If the first expression is not null, the COALESCE function
FROM employees;
returns that expression; otherwise, it does a COALESCE of
the remaining expressions.

1 2 3

4-7 Copyright © 2009, Oracle. All rights reserved. 4-8 Copyright © 2009, Oracle. All rights reserved.

Using the COALESCE Function Lesson Agenda

SELECT last_name, employee_id,


COALESCE(TO_CHAR(commission_pct),TO_CHAR(manager_id) • Implicit and explicit data type conversion
, • TO_CHAR, TO_DATE, TO_NUMBER functions
'No commission and no manager')
FROM employees; • Nesting functions
• General functions:
– NVL
– NVL2
– NULLIF
– COALESCE
• Conditional expressions:
… – CASE
– DECODE


4-9 Copyright © 2009, Oracle. All rights reserved. 4 - 10 Copyright © 2009, Oracle. All rights reserved.

Conditional Expressions CASE Expression

• Provide the use of the IF-THEN-ELSE logic within a SQL Facilitates conditional inquiries by doing the work of an
statement IF-THEN-ELSE statement:
• Use two methods:
CASE expr WHEN comparison_expr1 THEN return_expr1
– CASE expression
[WHEN comparison_expr2 THEN return_expr2
– DECODE function WHEN comparison_exprn THEN return_exprn
ELSE else_expr]
END

4 - 11 Copyright © 2009, Oracle. All rights reserved. 4 - 12 Copyright © 2009, Oracle. All rights reserved.
Using the CASE Expression DECODE Function

Facilitates conditional inquiries by doing the work of an Facilitates conditional inquiries by doing the work of a CASE
IF-THEN-ELSE statement: expression or an IF-THEN-ELSE statement:
SELECT last_name, job_id, salary,
CASE job_id WHEN 'IT_PROG' THEN 1.10*salary DECODE(col|expression, search1, result1
WHEN 'ST_CLERK' THEN 1.15*salary [, search2, result2,...,]
WHEN 'SA_REP' THEN 1.20*salary [, default])
ELSE salary END "REVISED_SALARY"
FROM employees;


4 - 13 Copyright © 2009, Oracle. All rights reserved. 4 - 14 Copyright © 2009, Oracle. All rights reserved.

Using the DECODE Function Using the DECODE Function

Display the applicable tax rate for each employee in department


SELECT last_name, job_id, salary, 80:
DECODE(job_id, 'IT_PROG', 1.10*salary,
'ST_CLERK', 1.15*salary, SELECT last_name, salary,
'SA_REP', 1.20*salary, DECODE (TRUNC(salary/2000, 0),
salary) 0, 0.00,
REVISED_SALARY 1, 0.09,
FROM employees; 2, 0.20,
3, 0.30,
4, 0.40,
… 5, 0.42,
6, 0.44,
0.45) TAX_RATE
… FROM employees
WHERE department_id = 80;

4 - 15 Copyright © 2009, Oracle. All rights reserved. 4 - 16 Copyright © 2009, Oracle. All rights reserved.

Summary

In this lesson, you should have learned how to:


• Alter date formats for display using functions
• Convert column data types using functions
• Use NVL functions
• Use IF-THEN-ELSE logic and other conditional
expressions in a SELECT statement

4 - 17 Copyright © 2009, Oracle. All rights reserved.

You might also like