1. Trang chủ
  2. » Công Nghệ Thông Tin

The Language of SQL- P12 pps

5 243 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 96,07 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

It looks like: SELECT RAND AS 'Random Value' If the seed argument is specified, it needs to be an integer value.. When theRAND function is executed with a seed argument provided, it wi

Trang 1

be used If it’s not used, then theRANDfunction returns a random value between

0 and 1 If executed 10 times in a row, it will return 10 different values It

looks like:

SELECT

RAND ( ) AS 'Random Value'

If the seed argument is specified, it needs to be an integer value When theRAND

function is executed with a seed argument provided, it will return the same value

every time It might look like:

SELECT

RAND (100) AS 'Random Value'

When you change the value of the seed argument, it will return a different value.

The PI function merely returns the value of the mathematical number pi

(Think back to your days in geometry class.) In reality, this function is very

sel-dom used, but it nicely illustrates the point that numeric functions need not have

any arguments For example, the statement:

SELECT PI ( )

returns the value 3.14159265358979

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

Oracle doesn’t have functions comparable to RAND or PI.

What if you wanted the value of pi rounded to only two decimal places? Simple

You merely create a composite function with thePIandROUNDfunctions You

would first use thePIfunction to get the initial value and then apply theROUND

function to round it to two decimal places The following statement returns a

value of 3.14:

SELECT ROUND (PI ( ), 2)

Conversion Functions

All of the aforementioned functions relate to specific ways to manipulate

char-acter, date/time, or numeric datatypes But you may need to convert data from

Trang 2

one datatype to another or convert NULL values to something meaningful The remainder of this chapter will cover two special functions that can be used in these situations

The CAST function allows you to convert data from one datatype to another The general format of the function is:

CAST (Expression AS DataType)

The CAST function is actually unnecessary in many situations Let’s take the situation where you want to execute this statement, where the Quantity column

is defined as a character column:

SELECT

2 * Quantity

FROM table

You might think that the statement would fail due to the fact that Quantity is not defined as a numeric column However, most SQL databases are smart enough

to automatically convert the Quantity column to a numeric value so it can be multiplied by 2

Here’s an example where you may need to use theCASTfunction Let’s say that you have a column with dates stored in a character column You would like to convert those dates to a true date/time column This statement illustrates how theCASTfunction can handle that conversion:

SELECT

'2009-04-11 0AS 'Original Date',

CAST ('2009-04-11 0AS DATETIME) AS 'Converted Date'

The output is:

Original Date Converted Date

2009-04-11 2009-04-11 00:00:00

The Original Date column looks like a date, but it is really just character data In contrast, the Converted Date column is a true date/time column, as evidenced by the time value, which is now shown

Chapter 4 ■ Using Functions

42

Trang 3

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

The equivalent statement for the previous CAST function in Oracle is:

SELECT

'2009-04-11 0AS "Original Date",

CAST ('11-APR-2009 0AS DATE) AS "Converted Date"

FROM DUAL;

A second useful conversion function is one that converts NULL values to a

meaningful value In Microsoft SQL Server, this function is calledISNULL

As mentioned in Chapter 1, NULL values are those for which there is an absence

of data A NULL value is not the same as a space or zero Let’s say that you have

this table of products:

Product Description Color

Notice that Chair B has a value of NULL in the Color column This indicates that

a color for this chair has not yet been provided Let’s say that you want to

pro-duce a list of all products If you issue thisSELECT:

SELECT

Description,

Color

FROM Products

It will show:

Description Color

Trang 4

However, users may prefer to see something such as ‘‘Unknown’’ rather than NULL for missing colors Here’s the solution:

SELECT

Description,

ISNULL (Color, 'Unknown') AS 'Color'

FROM Products

The following data is retrieved:

Description Color

D A T A B A S E D I F F E R E N C E S : M y S Q L a n d O r a c l e

The ISNULL function is called IFNULL in MySQL The equivalent of the above statement in MySQL is:

SELECT

Description,

IFNULL (Color, 'Unknown') AS 'Color'

FROM Products;

The ISNULL function is called NVL in Oracle The equivalent statement is:

SELECT

Description,

NVL (Color, 'Unknown') AS Color

FROM Products;

Additionally, unlike Microsoft SQL Server and MySQL, Oracle displays a dash rather than the word NULL when it encounters NULL values.

Looking Ahead

This chapter described a wide variety of functions, which are basically predefined rules for transforming a set of values into another value Just as spreadsheets provide built-in functions for manipulating data, SQL provides similar cap-abilities In addition to covering basic character, date/time, numeric, and

Chapter 4 ■ Using Functions

44

Trang 5

conversion functions, I’ve also explained how to create composite functions

from two or more of these functions

A lot of the material on functions is necessarily dry, due to the fact that there

are simply so many available functions with widely varying capabilities It’s

impossible to discuss every nuance of every available function The thing to

remember is that functions can be looked up easily in a database’s reference

guide when they need to be used Online reference material can serve as a handy

resource for exactly how each function works So when you need to use any

particular function, you can simply check online to verify the function’s syntax

In our next chapter, we’re going to take a break from columnlist issues and talk

about something a little more fun—how to sort your data Sorts can serve lots of

useful purposes and satisfy the basic desire of users to view data in some sort

of order With the sort, we will begin to think of the entire way in which our

information is presented, rather than with just bits and pieces of individual

data items

Ngày đăng: 05/07/2014, 05:20