Beginning SQL

Beginning SQL Beginning SQL

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

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)),

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)),

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

Saved successfully!

Ooh no, something went wrong!