The syntax for the previous statement in Oracle is: SELECT Orders.LastName FROM Orders123 Orders; Two remaining reasons for using table aliases will be covered in Chapters 11 and 14: ■ S
Trang 1Table Aliases
In addition to providing alternate names for columns, aliases can also be speci-fied for tables, using the sameAS keyword There are three general reasons for using table aliases
The first reason relates to tables with obscure or complex names For example, if
a table is named Orders123, you can use the followingSELECTto give it an alias
of Orders
SELECT
LastName
FROM Orders123 AS Orders
Unlike column aliases, table aliases are not enclosed in quotes When using table aliases, you have the option of using the alias as a prefix for any selected columns For example, the above could also be written as:
SELECT
Orders.LastName
FROM Orders123 AS Orders
The prefix Orders has now been added as a prefix to LastName, using a period to separate the prefix from the column name In this situation, the prefix wasn’t necessary However, when data is selected from multiple tables, the prefix will sometimes be required This will be seen in later chapters
D A T A B A S E D I F F E R E N C E S : O r a c l e
In Oracle, table aliases are specified without the AS keyword The syntax for the previous statement
in Oracle is:
SELECT
Orders.LastName
FROM Orders123 Orders;
Two remaining reasons for using table aliases will be covered in Chapters 11 and 14:
■ Situations when selecting from multiple tables
■ Situations when using a subquery in aSELECTstatement
Chapter 3 ■ Calculations and Aliases
26
Trang 2The meaning of the term subquery will become clear in Chapter 14 when the topic
is covered in detail
Looking Ahead
In this chapter, you learned about three general ways to create calculated fields in
aSELECT statement First, literal values can be used to select specific words or
values Second, arithmetic calculations can be used to perform calculations on
single or multiple columns Third, concatenation can be used to combine
col-umns and literal values together We also discussed the related topic of column
aliases, which are often employed when using calculated fields
In the next chapter, we’ll be moving on to the subject of functions, which provide
a slightly more complex way to perform calculations As mentioned before, we’re
not quite at the point where you can apply selection criteria to your statements
I’m still building on the basics of what can be done with the columnlist in a
SELECT Don’t worry We’ll get to the exciting stuff soon enough In the
meantime, your patience in sticking with this methodical approach will soon
pay off
Looking Ahead 27
Trang 3This page intentionally left blank
Trang 4chapter 4
Using Functions
Keywords Introduced: LEFT , RIGHT ,
SUBSTRING , LTRIM , RTRIM , CONCAT , UPPER ,
RAND , PI , CAST , ISNULL/IFNULL/NVL
For those of you familiar with spreadsheet software such as Microsoft Excel, you know that functions provide a huge amount of functionality for the typical spreadsheet user Without the ability to use functions, most of the data available
in spreadsheets would be of limited value The same is true in the world of SQL Your familiarity with some of the most commonly used SQL functions will greatly enhance your ability to generate dynamic results for those who will be using your reports
This chapter covers a wide variety of some of the most commonly used functions in four different categories: character functions, date/time functions, numeric tions, and conversion functions Additionally, we will talk about composite func-tions, which are a way of combining multiple functions into a single expression
The Function of Functions
Similar to the calculations covered in the previous chapter, functions provide another way to manipulate data As was seen, calculations involve multiple fields, either with arithmetic operators such as multiplication or by concatenation In contrast, functions are often performed on a single column
29
Trang 5What is a function? A function is merely a rule for transforming a value (or values) into another value, using a specific formula For example, the function
SUBSTRINGcan be used to determine that the first initial of the name JOAN is J
There are two types of functions: scalar and aggregate The term scalar comes
from mathematics and refers to an operation that is done on a single number In computer usage, it means that the function is performed on data in a single row For example, the LTRIM function removes leading spaces from one specified value
In contrast, aggregate functions are meant to be performed on a larger set of data For example, theSUM function can be used to calculate the sum of all the values of a specified column Since aggregate functions apply to sets or groups of data, we will leave our discussion of them to Chapter 10
Every SQL database offers dozens of scalar functions The functions vary widely between databases, in terms of their names and also how they work As a result,
we will only cover a few representative examples of some of the more useful functions
The most common types of scalar functions can be classified under three cate-gories: character, date/time, and numeric Obviously, these are functions that allow you to manipulate character, date/time, or numeric datatypes
In addition, you will learn about some useful conversion functions that can be used to convert data from one datatype to another
Character Functions
Character functions are those functions that enable you to manipulate character
data Just as character datatypes are sometimes called string datatypes, character functions are sometimes called string functions I’m going to cover these eight
examples of character functions:LEFT, RIGHT, SUBSTRING, LTRIM, RTRIM,
CONCAT,UPPER, andLOWER
In this chapter, rather than retrieve data from specified tables, I’m going to simply use SELECT statements with literal values Let’s start with our first example, which is for theLEFTfunction When you issue this SQL command:
SELECT
LEFT ('sunlight',3) AS 'The Answer'
Chapter 4 ■ Using Functions
30