Tải bản đầy đủ (.pdf) (5 trang)

The Language of SQL- P9 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (125.34 KB, 5 trang )

Table Aliases
In addition to providing alternate names for columns, aliases can also be speci-
fied for tables, using the same
AS 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 following
SELECT to 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.
DATABASE DIFFERENCES: Oracle
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 a SELECT statement
Chapter 3

Calculations and Aliases26
The 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
a
SELECT 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
This page intentionally left blank
chapter 4
Using Functions
Keywords Introduced: LEFT, RIGHT,
SUBSTRING, LTRIM, RTRIM, CONCAT, UPPER,
LOWER, GETDATE/NOW/CURRENT_DATE,
DATEPART/DATE_FORMAT, DATEDIFF, ROUND,

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 chapte r c ove r s a wide variety of s om e of t he m os t com monl y u se d fun ction s i n
four different categories: character functions, date/time functions, numeric func-
tions, and con ve rs ion functions. Additiona l ly, we will talk ab out composite fun c -
tions, which ar e a way o f com bin in g mu ltipl e f un ction s in to a sin gl e ex pr ess ion .
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
What 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
SUBSTRING can 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, the
SUM 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, and LOWER.
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 the
LEFT function. When you issue this SQL command:
SELECT
LEFT ('sunlight',3) AS 'The Answer'
Chapter 4

Using Functions30

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×