20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Finally, if you want to use an alias that contains spaces or any other characters normally not permitted for<br />

column names or aliases, then you must enclose the alias name inside square brackets, as shown below:<br />

SELECT LastName AS Surname, FirstName AS [Christian Name]<br />

FROM MemberDetails;<br />

Using this <strong>SQL</strong>, the alias Christian Name has a space, so it’s enclosed inside the square brackets.<br />

Square brackets allow you to use names for columns or aliases that contain characters not normally considered<br />

legal. For example, you would receive an error if you tried to use the alias One***Two, as shown<br />

in the following statement:<br />

SELECT DateOfBirth AS One***Two FROM MemberDetails;<br />

But if you put square brackets around the alias, the database system is happy:<br />

SELECT DateOfBirth AS [One***Two] FROM MemberDetails;<br />

Both Oracle and DB2 support a second way of concatenating: the CONCAT() function. You pass the two<br />

things you want to join, either columns or literal strings, as arguments to the function, and the function<br />

returns them joined. For example, to join FirstName and LastName columns, you would write the following<br />

query:<br />

SELECT CONCAT(FirstName, LastName) FROM MemberDetails;<br />

Executing this query produces the following results:<br />

KatieSmith<br />

SusieSimons<br />

JohnJackson<br />

SteveGee<br />

JohnJones<br />

JennyJones<br />

JackJohnson<br />

SeymourBotts<br />

JamieHills<br />

StuartDales<br />

WilliamDoors<br />

DorisNight<br />

Although CONCAT() does the same thing in Oracle and DB2, there are plenty of subtle differences. One<br />

difference is that Oracle does its best to convert values into text. Although the following statement works<br />

on Oracle, it produces an error in DB2:<br />

SELECT CONCAT(DateOfBirth, LastName) FROM MemberDetails;<br />

Extracting Information<br />

87

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

Saved successfully!

Ooh no, something went wrong!