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 3<br />

Executing this <strong>SQL</strong> provides the following results:<br />

LastName FirstName DateOfBirth<br />

Night Doris 1997-05-28<br />

Doors William 1994-05-28<br />

Hills Jamie 1992-07-17<br />

Smith Katie 1977-01-09<br />

Jackson John 1974-05-27<br />

Gee Steve 1967-10-05<br />

Botts Seymour 1956-10-21<br />

Dales Stuart 1956-08-07<br />

Jones Jenny 1953-08-25<br />

Jones John 1952-10-05<br />

Johnson Jack 1945-06-09<br />

Simons Susie 1937-01-20<br />

The next section looks at how columns in the results set can be joined together, for example, returning<br />

FirstName and LastName columns as one column called FullName.<br />

Joining Columns — Concatenation<br />

Not only does <strong>SQL</strong> allow you to query various columns, but it also allows you to combine one or more<br />

columns and give the resulting column an alias. Note that using an alias has no effect on the table itself;<br />

you’re not really creating a new column in the table, only one for the results set. When you join columns<br />

together, you concatenate them. For example, if you have the data ABC and concatenate it to DEF, you get<br />

ABCDEF.<br />

This chapter only attempts to concatenate text literals or columns that have the char or varchar data<br />

type. Joining text with a number can cause errors. Chapter 5 shows you how to convert data types and<br />

use this to convert numbers to text and then concatenate.<br />

So how do you go about concatenating text? Unfortunately, concatenating text varies depending on the<br />

database system you’re using. Because there are significant differences among the five database systems<br />

covered in this chapter, each is taken in turn, starting with <strong>SQL</strong> Server and MS Access. You simply need<br />

to read the section relevant to the database system you’re using — feel free to skip over the others.<br />

MS <strong>SQL</strong> Server and MS Access<br />

82<br />

Both <strong>SQL</strong> Server and Access concatenate columns and data in the same manner — with the concatenation<br />

operator, which on these systems is the plus (+) sign. So, in order to return the full name, that is first<br />

and last name, of members, you’d write the following:<br />

SELECT FirstName + ‘ ‘ + LastName AS FullName FROM MemberDetails;

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

Saved successfully!

Ooh no, something went wrong!