Monday, October 22, 2007

Nesting Functions

Nesting Functions
• Single-row functions can be nested to any level.
• Nested functions are evaluated from deepest level to the least deep level.

General Functions
These functions work with any data type and pertain to using null value.
• NVL (expr1, expr2)
• NVL2 (expr1, expr2, expr3)
• NULLIF (expr1, expr2)
• COALESCE (expr1, expr2, ..., exprn).

NVL Function
• Converts a null to an actual value.
• Data types that can be used are date, character, and number.
• Data types must match:
– NVL(commission_pct,0)
– NVL(hire_date,'01-JAN-97')
– NVL(job_id,'No Job Yet')

Using the NVL Function

Using the NVL2 Function

Using the NULLIF Function


Using the COALESCE Function
• The advantage of the COALESCE function over the NVL function is that the COALESCE function can take multiple alternate values.
• If the first expression is not null, it returns that expression; otherwise, it does a COALESCE of the remaining expressions.

Using the COALESCE Function


Conditional Expressions
• Give you the use of IF-THEN-ELSE logic within a SQL statement
• Use two methods:
– CASE expression
– DECODE function

The CASE Expression
Facilitates conditional inquiries by doing the work of an IF-THEN-ELSE statement:

Using the CASE Expression
Facilitates conditional inquiries by doing the work of an IF-THEN-ELSE statement:

The DECODE Function
Facilitates conditional inquiries by doing the work of a CASE or IF-THEN-ELSE statement:

Using the DECODE Function.
Display the applicable tax rate for each employee in department 80.

Sunday, September 30, 2007

Conversion Functions

Overview
This practice covers the following topics:
• Writing a query that displays the current date
• Creating queries that require the use of numeric, character, and date functions.
Data-type conversion
Implicit Data-Type Conversion
For assignments, the Oracle server can automatically convert the following:
Implicit Data-Type ConversionFor expression evaluation, the Oracle Server can automatically convert the following:


Explicit Data-Type Conversion
Explicit Data-Type Conversion

Using the TO_CHAR Function with Dates
TO_CHAR Function
The format model:
• Must be enclosed in single quotation marks and is case sensitive
• Can include any valid date format element
• Has an fm element to remove padded blanks or suppress leading zeros
• Is separated from the date value by a comma.


Elements of the Date Format Model
Elements of the Date Format Model• Time elements format the time portion of the date.
• Add character strings by enclosing them in double quotation marks.
• Number suffixes spell out numbers.


Using the TO_CHAR Function with Dates
TO_CHAR Function

Using the TO_CHAR Function with Numbers
TO_CHAR Function with NumbersThese are some of the format elements you can use with the TO_CHAR function to display a number value as a character:


Using the TO_NUMBER and TO_DATE Functions
• Convert a character string to a number format using the TO_NUMBER function:
TO_NUMBER function• Convert a character string to a date format using the TO_DATE function:
TO_DATE function• These functions have an fx modifier. This modifier specifies the exact matching for the character argument and date format model of a TO_DATE function.


RR Date Format

RR Date FormatExample of RR Date Format

Tuesday, September 25, 2007

Single-Row Functions

Objectives
After completing this lesson, you should be able to do the following:
• Describe various types of functions available in SQL
• Use character, number, and date functions in SELECT statements
• Describe the use of conversion functions.


SQL Functions


Two Types of SQL Functions



Single-Row Functions

Single row functions:
• Manipulate data items
• Accept arguments and return one value
• Act on each row returned
• Return one result per row
• May modify the data type
• Can be nested
• Accept arguments which can be a column or an expression.

Character Functions

Case Manipulation Functions
These functions convert case for character strings.

Using Case Manipulation Functions
Display the employee number, name, and department number for employee Higgins:

Character-Manipulation Functions
These functions manipulate character strings:


Using the Character-Manipulation
Functions


Number Functions

• ROUND: Rounds value to specified decimal
• TRUNC: Truncates value to specified decimal
• MOD: Returns remainder of division


Using the ROUND Function
DUAL is a dummy table you can use to view results from functions and calculations.

Using the TRUNC Function

Using the MOD Function

Calculate the remainder of a salary after it is divided by 5000 for all employees whose job title is sales representative.


Working with Dates

• Oracle database stores dates in an internal numeric format: century, year, month, day, hours,
minutes, seconds.
• The default date display format is DD-MON-RR.
– Allows you to store 21st century dates in the 20th century by specifying only the last two digits of the year.
– Allowa you to store 20th century dates in the 21st century in the same way.

SYSDATE is a function that returns:
• Date
• Time

Arithmetic with Dates
• Add or subtract a number to or from a date for a resultant date value.
• Subtract two dates to find the number of days between those dates.
• Add hours to a date by dividing the number of hours by 24.

Using Arithmetic Operators with Dates

Date Functions



Saturday, September 22, 2007

Restricting and Sorting Data

Objectives
After completing this lesson, you should be able to do the following:
• Limit the rows retrieved by a query
• Sort the rows retrieved by a query

Limiting Rows Using a Selection


Limiting the Rows Selected
• Restrict the rows returned by using the WHERE clause.
• The WHERE clause follows the FROM clause.

Using the WHERE Clause

Character Strings and Dates
• Character strings and date values are enclosed in single quotation marks.
• Character values are case sensitive, and date values are format sensitive.
• The default date format is DD-MON-RR.

Comparison Conditions

Using Comparison Conditions
Other Comparison Conditions

Using the BETWEEN Condition

Using the IN Condition


Using the LIKE Condition
• Use the LIKE condition to perform wild card searches of valid search string values.
• Search conditions can contain either literal characters or numbers:
– % denotes zero or many characters.
– _ denotes one character.
• You can combine pattern-matching characters.
• You can use the ESCAPE identifier to search for the actual % and _ symbols.


Using the NULL Conditions
Test for nulls with the IS NULL operator.

Logical Conditions

Using the AND Operator
AND requires both conditions to be true.

Using the OR Operator
OR requires either condition to be true.

Using the NOT Operator


Rules of Precedence
Override rules of precedence by using parentheses.
Use parentheses to force priority.

ORDER BY Clause
• Sort rows with the ORDER BY clause
– ASC: ascending order (the default order)
– DESC: descending order
• The ORDER BY clause comes last in the SELECT statement.

Sorting in Descending Order

Sorting by Column Alias

Sorting by Multiple Columns
• The order of ORDER BY list is the order of sort.
• You can sort by a column that is not in the SELECT list.

Summary
In this, you should have learned how to:

• Use the WHERE clause to restrict rows of output
– Use the comparison conditions
– Use the BETWEEN, IN, LIKE, and NULL conditions
– Apply the logical AND, OR, and NOT operators

• Use the ORDER BY clause to sort rows of output