Beginning SQL
Beginning SQL Beginning SQL
Chapter 5 184 SELECT FirstName, LastName, City, COALESCE(FirstName + ‘ ‘ + LastName + ‘ lives in ‘ + City, ‘We have no details for ‘ + FirstName + ‘ ‘ + LastName, ‘No data available’) FROM MemberDetails; For the other database systems — IBM DB2, MySQL, and Oracle — the query should read as follows: SELECT FirstName, LastName, City, COALESCE( CONCAT( CONCAT(FirstName,’ ‘), CONCAT(CONCAT(LastName, ‘ lives in ‘), City)), CONCAT(‘We have no details for ‘,CONCAT(CONCAT(FirstName,’ ‘),LastName)), ‘No data available’) FROM MemberDetails; Running either query gives the following results: FirstName LastName City COALESCE( CONCAT( CONCAT(FirstName,’ ‘), CONCAT(CONCAT(LastName, ‘ lives in ‘), City)), CONCAT(‘We have no details for ‘,CONCAT(CONCAT(FirstName,’ ‘),LastName)), ‘No data available’) Katie Smith Townsville Katie Smith lives in Townsville Steve Gee New Town Steve Gee lives in New Town John Jones Orange Town John Jones lives in Orange Town Jenny Jones Orange Town Jenny Jones lives in Orange Town John Jackson Orange Town John Jackson lives in Orange Town Jack Johnson Big City Jack Johnson lives in Big City Seymour Botts Windy Village Seymour Botts lives in Windy Village Susie Simons Townsville Susie Simons lives in Townsville Jamie Hills Orange Town Jamie Hills lives in Orange Town Stuart Dales Windy Village Stuart Dales lives in Windy Village William Doors Big City William Doors lives in Big City Doris Night Dover Doris Night lives in Dover Catherine Hawthorn NULL We have no details for Catherine Hawthorn To explain this relatively complex SQL, consider the three arguments passed to the COALESCE() function. The first argument appears below: CONCAT( CONCAT(FirstName,’ ‘), CONCAT(CONCAT(LastName, ‘ lives in ‘), City)),
- Page 358: The results of this query are as fo
- Page 362: You can raise a number to any power
- Page 366: To demonstrate the sort of results
- Page 370: The CEILING() Function The CEILING(
- Page 374: ased on the digits after the decima
- Page 378: The SUBSTRING() function works at t
- Page 382: LastName UPPER(LastName) LOWER(Last
- Page 386: The LENGTH() function takes just on
- Page 390: LastName LOWER(SUBSTRING(LastName,1
- Page 394: FirstName DIFFERENCE(FirstName, ‘
- Page 398: Converting Different Data Types Thi
- Page 402: CREATE Table MyTable(FirstColumn in
- Page 406: The COALESCE() Function The COALESC
- Page 412: Chapter 5 186 This SQL copies over
- Page 416: Chapter 5 You also learned that mat
- Page 420: Chapter 6 190 Begin by looking at h
- Page 424: Chapter 6 This section covers queri
- Page 428: Chapter 6 194 This query gives the
- Page 432: Chapter 6 You’re almost there. Yo
- Page 436: Chapter 6 the database even though
- Page 440: Chapter 6 SELECT Category, AVG(DVDP
- Page 444: Chapter 6 Try It Out Selecting Data
- Page 448: Chapter 6 SELECT Category, COUNT(Fa
- Page 454: 7 Selecting Data from Different Tab
Chapter 5<br />
184<br />
SELECT FirstName,<br />
LastName, City,<br />
COALESCE(FirstName + ‘ ‘ + LastName + ‘ lives in ‘ + City,<br />
‘We have no details for ‘ + FirstName + ‘ ‘ + LastName,<br />
‘No data available’)<br />
FROM MemberDetails;<br />
For the other database systems — IBM DB2, My<strong>SQL</strong>, and Oracle — the query should read as follows:<br />
SELECT FirstName, LastName, City,<br />
COALESCE(<br />
CONCAT( CONCAT(FirstName,’ ‘), CONCAT(CONCAT(LastName, ‘ lives in ‘), City)),<br />
CONCAT(‘We have no details for ‘,CONCAT(CONCAT(FirstName,’ ‘),LastName)),<br />
‘No data available’)<br />
FROM MemberDetails;<br />
Running either query gives the following results:<br />
FirstName LastName City COALESCE(<br />
CONCAT( CONCAT(FirstName,’ ‘),<br />
CONCAT(CONCAT(LastName, ‘ lives in ‘), City)),<br />
CONCAT(‘We have no details for<br />
‘,CONCAT(CONCAT(FirstName,’ ‘),LastName)),<br />
‘No data available’)<br />
Katie Smith Townsville Katie Smith lives in Townsville<br />
Steve Gee New Town Steve Gee lives in New Town<br />
John Jones Orange Town John Jones lives in Orange Town<br />
Jenny Jones Orange Town Jenny Jones lives in Orange Town<br />
John Jackson Orange Town John Jackson lives in Orange Town<br />
Jack Johnson Big City Jack Johnson lives in Big City<br />
Seymour Botts Windy Village Seymour Botts lives in Windy Village<br />
Susie Simons Townsville Susie Simons lives in Townsville<br />
Jamie Hills Orange Town Jamie Hills lives in Orange Town<br />
Stuart Dales Windy Village Stuart Dales lives in Windy Village<br />
William Doors Big City William Doors lives in Big City<br />
Doris Night Dover Doris Night lives in Dover<br />
Catherine Hawthorn NULL We have no details for Catherine Hawthorn<br />
To explain this relatively complex <strong>SQL</strong>, consider the three arguments passed to the COALESCE() function.<br />
The first argument appears below:<br />
CONCAT( CONCAT(FirstName,’ ‘), CONCAT(CONCAT(LastName, ‘ lives in ‘), City)),