be used. If it’s not used, then the RAND function 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 the RAND 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 DATABASE DIFFERENCES: Oracle 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 the PI and ROUND functions. You would first use the PI function to get the initial value and then apply the ROUND 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 Conversion Functions 41 one datatype to another or conv ert 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 the CAST function. 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 the CAST function can handle that conversion: SELECT '2009-04-11 0 AS 'Original Date', CAST ('2009-04-11 0 AS DATETIME) AS 'Converted Date' The outpu t 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 Functions42 DATABASE DIFFERENCES: Oracle The equivalent statement for the previous CAST function in Oracle is: SELECT '2009-04-11 0 AS "Original Date", CAST ('11-APR-2009 0 AS 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 called ISNULL. 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 1 Chair A Red 2 Chair B NULL 3 Lamp C Green 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 this SELECT: SELECT Description, Color FROM Products It will show: Description Color Chair A Red Chair B NULL Lamp C Green Conversion Functions 43 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 Chair A Red Chair B Unknown Lamp C Green DATABASE DIFFERENCES: MySQL and Oracle 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 Functions44 conversion functions, I’ve also explained how to create compo site 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. Looking Ahead 45 . '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. 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. 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