20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5<br />

The syntax for the CAST() function consists of the expression to be cast and what data type you want to<br />

cast it as:<br />

CAST(expression AS data_type)<br />

The code example at the beginning of this section doesn’t work with DB2 because ZipCode is a character<br />

data type column, and DB2 doesn’t automatically try to convert the data type to a number, unlike<br />

other systems such as <strong>SQL</strong> Server.<br />

In order to rewrite the earlier example so that it works with IBM’s DB2, you need to cast the ZipCode<br />

column as an int data type, as illustrated in the following <strong>SQL</strong>, which will work with all the database<br />

systems including DB2:<br />

SELECT ZipCode, MemberId, CAST(ZipCode AS int) / MemberId<br />

FROM MemberDetails;<br />

Note that CAST() works only with later versions of My<strong>SQL</strong>, from 4.02 onward, and that only some of the<br />

data types can be cast. To make the preceding code work in My<strong>SQL</strong>, you need to change it to the following:<br />

SELECT ZipCode, MemberId, CAST(ZipCode AS signed integer) / MemberId<br />

FROM MemberDetails;<br />

You can convert most data types to another data type, so long as the data can be converted. For example,<br />

ZipCode contains digits, so conversion to an integer data type works fine. If you try converting ZipCode<br />

to a date data type, as shown in the following <strong>SQL</strong>, you would receive errors because the values contained<br />

in ZipCode simply can’t be converted to a date:<br />

SELECT ZipCode, MemberId, CAST(ZipCode AS date)<br />

FROM MemberDetails;<br />

MS Access <strong>SQL</strong> doesn’t support CAST(), but its Visual Basic language does have a number of conversion<br />

functions, which are beyond the scope of this book.<br />

This section covered how to convert values of a specific data type to another data type. The next section<br />

returns to the problematic question of the NULL data type.<br />

Re-examining NULL<br />

NULLs can be a bit problematic and can lead to unexpected results when used with functions, in comparisons,<br />

or in math. Whereas Chapter 3 covered the concept of NULL being unknown data, this section covers<br />

some of the issues that NULLs can throw up when used with math functions, string functions, and<br />

concatenation. The section also introduces the NULLIF() and COALESCE() functions.<br />

NULLs and Math<br />

180<br />

If NULL appears in any of your <strong>SQL</strong> math, then the result is always NULL. To illustrate this point, use the<br />

following <strong>SQL</strong> to create a temporary table called MyTable and insert a few NULL values:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!