Thursday, October 18, 2007

Single Row Function In SQL SERVER

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


Basic Select Statement

SELECT Statement
Basic SELECT Statement

Selecting All Columns
Selecting Specific Columns
Writing SQL Statements
• SQL statements are not case sensitive.
• SQL statements can be on one or more lines.
• Keywords cannot be abbreviated or split
across lines.
• Clauses are usually placed on separate lines.
• Indents are used to enhance readability.

Column Heading Defaults
• iSQL*Plus:
– Default heading justification: Center
– Default heading display: Uppercase
• SQL*Plus:
– Character and Date column headings are leftjustified
– Number column headings are right-justified
– Default heading display: Uppercase

Arithmetic Expressions
Create expressions with number and date data by using arithmetic operators.
Using Arithmetic Operators

Operator Precedence
• Multiplication and division take priority over addition and subtraction.
• Operators of the same priority are evaluated from left to right.
• Parentheses are used to force prioritized evaluation and to clarify statements.


Using Parentheses

Defining a Null Value
• A null is a value that is unavailable, unassigned,unknown, or inapplicable.
• A null is not the same as zero or a blank space.


Null Values in Arithmetic Expressions
Arithmetic expressions containing a null value evaluate to null.


Defining a Column Alias
A column alias:
• Renames a column heading
• Is useful with calculations
• Immediately follows the column name: there can also be the optional AS keyword between the column name and alias
• Requires double quotation marks if it contains spaces or special characters or is case sensitive.

Using Column Aliases

Concatenation Operator
A concatenation operator:
• Concatenates columns or character strings to other columns
• Is represented by two vertical bars (||)
• Creates a resultant column that is a character expression.


Using the Concatenation Operator

Literal Character Strings
• A literal value is a character, a number, or a date included in the SELECT list.
• Date and character literal values must be enclosed within single quotation marks.
• Each character string is output once for each row returned.

Using Literal Character Strings

Duplicate Rows
The default display of queries is all rows, including duplicate rows.
Eliminating Duplicate Rows
Eliminate duplicate rows by using the "DISTINCT" keyword in the "SELECT" clause.
SQL and iSQL*Plus Interaction

SQL Statements versus iSQL*Plus Commands


Summary

In this lesson, you should have learned how to:
• Write a SELECT statement that:
– Returns all rows and columns from a table
– Returns specified columns from a table
– Uses column aliases to give descriptive columnheadings
• Use the iSQL*Plus environment to write, save, and execute SQL statements and iSQL*Plus commands.