Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

Chapter 3 Even though DateOfBirth is a date column and not a character data type, if you try to execute the same query in DB2, you get an error. For most of this book, you’ll find it easier to use the double vertical pipe (||) to concatenate data. That covers concatenation in Oracle and DB2. Now it’s time to look at MySQL’s way of concatenating. MySQL 88 MySQL concatenates using one of two functions. The first is the CONCAT() function, which works in a way similar to CONCAT() used with Oracle and DB2. However, unlike under those database systems, it can take two or more arguments. So, if you want to join three columns, you would write a query similar to the following: SELECT CONCAT(MemberId,FirstName,LastName) FROM MemberDetails; Executing the query gives these results: 1KatieSmith 4SteveGee 5JohnJones 6JennyJones 7JohnJackson 8JackJohnson 9SeymourBotts 10SusieSimons 11JamieHills 12StuartDales 13WilliamDoors 14DorisNight Notice that a numeric data type column is concatenated. MySQL’s CONCAT() function will, if possible, convert numeric data types to string values before concatenating. As well as columns, CONCAT() can also join string literals. Consider the following code: SELECT CONCAT(‘The member is called ‘,FirstName,’ ‘,LastName) AS ‘Member Name’ FROM MemberDetails; Executing the preceding query creates the following results:

Chapter 3<br />

Even though DateOfBirth is a date column and not a character data type, if you try to execute the<br />

same query in DB2, you get an error. For most of this book, you’ll find it easier to use the double vertical<br />

pipe (||) to concatenate data.<br />

That covers concatenation in Oracle and DB2. Now it’s time to look at My<strong>SQL</strong>’s way of concatenating.<br />

My<strong>SQL</strong><br />

88<br />

My<strong>SQL</strong> concatenates using one of two functions. The first is the CONCAT() function, which works in a<br />

way similar to CONCAT() used with Oracle and DB2. However, unlike under those database systems, it<br />

can take two or more arguments. So, if you want to join three columns, you would write a query similar<br />

to the following:<br />

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

Executing the query gives these results:<br />

1KatieSmith<br />

4SteveGee<br />

5JohnJones<br />

6JennyJones<br />

7JohnJackson<br />

8JackJohnson<br />

9SeymourBotts<br />

10SusieSimons<br />

11JamieHills<br />

12StuartDales<br />

13WilliamDoors<br />

14DorisNight<br />

Notice that a numeric data type column is concatenated. My<strong>SQL</strong>’s CONCAT() function will, if possible,<br />

convert numeric data types to string values before concatenating.<br />

As well as columns, CONCAT() can also join string literals. Consider the following code:<br />

SELECT CONCAT(‘The member is called ‘,FirstName,’ ‘,LastName) AS ‘Member Name’ FROM<br />

MemberDetails;<br />

Executing the preceding query creates the following results:

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

Saved successfully!

Ooh no, something went wrong!