NULL Functions. 2 home back first prev next last What Will I Learn? Nested function List at least four general functions that work with any data type.

Slides:



Advertisements
Similar presentations
Objectives After completing this lesson, you should be able to do the following: Describe various types of conversion functions that are available in.
Advertisements

Copyright © 2007, Oracle. All rights reserved Using Single-Row Functions to Customize Output Modified: October 21, 2014.
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Database Design Sections 16 & 17 – Columns, Characters, Rows, Concatenations, DISTINCT, DESCRIBE, Logical Operators, Order of Operations, Sorting 9/26/2011.
Using Single-Row Functions to Customize Output
SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition.
Ceng 356-Lab1. Objectives After completing this lesson, you should be able to do the following: Get Familiar with the development environment List the.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
2-1 Copyright © Oracle Corporation, All rights reserved. Character Strings and Dates Character strings and date values are enclosed in single quotation.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Database Programming Sections 1 & 2 – Case and Character Manipulations, number functions, date functions, conversion functions, general functions, conditional.
Conversion Functions Implicit datatype conversion Explicit datatype conversion Datatypeconversion In some cases, Oracle Server allows data of one datatype.
Single – Row Functions. Objectives After completing this lesson, you should be able to do the following:  Describe various types of functions available.
DEFAULT Values and the MERGE Statement. 2 home back first prev next last What Will I Learn? Understand when to specify a DEFAULT value. Construct and.
Single Row Functions Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number, date, general and conversion.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
6 Copyright © 2006, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
2-1 Copyright © Oracle Corporation, All rights reserved. Using the NULL Conditions Test for nulls with the IS NULL operator. SELECT last_name, manager_id.
Database Design Sections 17 & 18 – Concatenations, DISTINCT, DESCRIBE, Logical Operators, Order of Operations, Sorting.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Conversion Functions.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
Retrieving Data Using the SQL SELECT Statement. Objectives After completing this lesson, you should be able to do the following: – List the capabilities.
Oracle 11g DATABASE DEVELOPMENT LAB2. Chapter- 2  These commands, which could be issued from SQL*Plus or SQL Developer,  will make it possible to log.
INSERT Statement. 2 home back first prev next last What Will I Learn? Give examples of why it is important to be able to alter the data in a database.
Chapter 12 Subqueries and Merge Statements
INTRODUCTION TO SQL Chapter SELECT * FROM teacher WHERE INSTR (subject_id, ‘&1’)= 4 AND LOWER (subject_id) LIKE ‘HST%’ ; When prompted for the.
Creating Views. 2 home back first prev next last What Will I Learn? List three uses for views from the standpoint of a database administrator Explain,
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
3 第三讲 Single-Row Functions. Objectives After completing this lesson, you should be able to do the following: Describe various types of functions available.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
Database Programming Sections 1 & 2 – Case and Character Manipulations, number functions, date functions, conversion functions, general functions, conditional.
SQL1-ch3 使用單列函數自訂輸出. 考古題題號  80 題: 7 、 37  140 題: 30 、 43 、 52 、 84 、 86 、 127.
Working with Columns, Characters, and Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Apply the concatenation.
Using Single-Row Functions to Customize Output
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
3 Copyright © 2007, Oracle. All rights reserved. Substitution Variables ra Oly l&On nase lce Int erU.
Database Design Sections 17 & 18 – Columns, Characters, Rows, Concatenations, DISTINCT, DESCRIBE, Logical Operators, Order of Operations, Sorting.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
3 Copyright © 2004, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
4 Copyright © 2009, Oracle. All rights reserved. Using Conversion Functions and Conditional Expressions.
Restricting and Sorting Data
Retrieving Data Using the SQL SELECT Statement
Aggregating Data Using Group Functions
Using Single-Row Functions to Customize Output
Using Single-Row Functions to Customize Output
(SQL) Aggregating Data Using Group Functions
Using the Set Operators
Restricting and Sorting Data
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Restricting and Sorting Data
Aggregating Data Using Group Functions
Presentation transcript:

NULL Functions

2 home back first prev next last What Will I Learn? Nested function List at least four general functions that work with any data type and relate to handling null values –NVL, NVL2, NULLIF, and COALESCE

3 home back first prev next last Why Learn It? Besides functions that control how data is formatted or converted to another type, SQL uses a set of general functions designed specifically to deal with null values. You may be wondering how a value that is unavailable, unassigned, unknown, or inapplicable can deserve so much attention. Null may be "nothing," but it can affect how expressions are evaluated, how averages are computed, and where a value appears in a sorted list. This lesson is all about handling null values.

4 home back first prev next last Nest functions It is possible to nest functions to any depth. The evaluation process begins from the innermost level to the outermost level. SELECT TO_CHAR(NEXT_DAY(ADD_MONTHS(hire_date, 6), 'FRIDAY'), 'fmDay, Month DDth, YYYY') AS "Next Evaluation" FROM employees WHERE employee_id=100; The result returned will appear as: Friday, December 18TH, 1987 The evaluation order is: ADD_MONTHS, NEXT_DAY, TO_CHAR

5 home back first prev next last Null and Null functions Null is the value that is unavailiable, unassigned, unknown, or inapplicable. It isn't equal to anything, not even zero! There are four general functions that pertain to the use of null values. –NVL –NVL2 –NULLIF –COALESCE

6 home back first prev next last NVL FUNCTION NVL function converts a null value to a date, a character, or a number. –The data types of the null value column and the new value must be the same. NVL ( value may be null, value to replace the null) SELECT NVL(comments, 'no comment') FROM D_PLAY_LIST_ITEMS;

7 home back first prev next last NVL FUNCTION The data types of the null value column and the new value must be the same as shown in the following examples: NVL(auth_expense_amt,0) NVL(hire_date,'01-JAN-97') NVL(specialty,'None Yet')

8 home back first prev next last NVL FUNCTION NVL functions can be used to convert column values containing nulls to a number before doing calculations. When arithmetic calculation is performed with null, the result is null. The NVL function can first convert the null value to a number before arithmetic calculations are done to avoid a null result.

9 home back first prev next last NVL FUNCTION In the example, the auth_expense_amt column in the D_PARTNERS table contains null values. The NVL function is used to change the null to zero before arithmetic calculations. SELECT first_name, last_name,NVL(auth_expense_amt, 0) * 1.05 AS Expenses FROM D_Partners;

10 home back first prev next last NVL2 FUNCTION The NVL2 function evaluates an expression with three values. NVL2 (expression 1 value that may contain a null, expression 2 value to return if expression 1 is not null, expression 3 value to return if expression 1 is null) The values in expression 1 can have any data type. Expression 2 and expression 3 can have any data type except LONG. The data type of the returned value is always the same as the data type of expression 2, unless expression 2 is character data, in which case the returned type is VARCHAR2.

11 home back first prev next last NVL2 FUNCTION An example SELECT last_name, salary, NVL2(commission_pct, salary + (salary *commission_pct), salary) income FROM employees; 注意:三个表达式数据类型可以不同; SELECT last_name, salary, NVL2(commission_pct, 'have comm', sysdate) income FROM employees; 虽然上面查询没有意义,但却是合法的。

12 home back first prev next last NULLIF FUNCTION NULLIF(expression 1, expression 2) The NULLIF function compares two values. –If they are equal, the function returns null. –If they are not equal, the function returns the first expression.

13 home back first prev next last NULLIF FUNCTION SELECT first_name, LENGTH(first_name) "Expression1", last_name, LENGTH(last_name) "Expression 2", NULLIF(LENGTH(first_name), LENGTH(last_name)) AS "Compare Them " FROM D_PARTNERS;

14 home back first prev next last COALESCE FUNCTION The COALESCE function is: COALESCE (value 1, value 2,... value n) The word "coalesce" literally means "to come together". –If the first expression is null, the function continues down the line until a not null expression is found. –If the first expression has a value, the function returns the first expression and the function stops.

15 home back first prev next last COALESCE FUNCTION SELECT last_name, COALESCE(commission_pct, salary, 10) comm FROM employees ORDER BY commission_pct; Which employees do not receive a commission? How can you tell? Is there anyone who receives neither a commission percentage nor a salary?

16 home back first prev next last Terminology Key terms used in this lesson include: –COALESCE –NULLIF –NVL –NVL2

17 home back first prev next last Summary Demonstrate and explain the evaluation of a nested function List at least four general functions that work with any data type and relate to handling null values Explain the use of the COALESCE and the NVL functions Explain the use of general functions to deal with null values in data Construct and execute a SQL query that correctly applies NVL, NVL2, NULLIF, and COALESCE single-row functions