Beginning SQL
Beginning SQL Beginning SQL
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:
- Page 164: Chapter 3 Logical Operators and Ope
- Page 168: Chapter 3 The statement returns the
- Page 172: Chapter 3 NOT Operator Examples thu
- Page 176: Chapter 3 2. Using the following SQ
- Page 180: Chapter 3 When you execute the quer
- Page 184: Chapter 3 Now that you’re acquain
- Page 188: Chapter 3 It’s as simple as that!
- Page 192: Chapter 3 76 YearReleased 1947 1967
- Page 196: Chapter 3 78 FilmName Rating Gone w
- Page 200: Chapter 3 2. Add a WHERE clause to
- Page 204: Chapter 3 Executing this SQL provid
- Page 208: Chapter 3 84 FullName First name is
- Page 212: Chapter 3 86 FullName First name is
- Page 218: Member Name The member is called Ka
- Page 222: FilmName YearReleased Rating Sense
- Page 226: Figure 3-1 The first task when tack
- Page 230: That’s all well and good, but so
- Page 234: Category FirstName LastName War Jen
- Page 238: Extracting Information FirstName La
- Page 242: If you take this further and join t
- Page 246: SELECT * FROM FavCategory Figure 3-
- Page 250: Category MemberId Thriller 5 Thrill
- Page 254: Remember to add brackets around the
- Page 258: surprisingly, the MemberDetails tab
- Page 262: LocationId 2 2 2 2 1 1 1 Figure 3-6
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: