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.

Member Name<br />

The member is called Katie Smith<br />

The member is called Steve Gee<br />

The member is called John Jones<br />

The member is called Jenny Jones<br />

The member is called John Jackson<br />

The member is called Jack Johnson<br />

The member is called Seymour Botts<br />

The member is called Susie Simons<br />

The member is called Jamie Hills<br />

The member is called Stuart Dales<br />

The member is called William Doors<br />

The member is called Doris Night<br />

Notice that the results returned by CONCAT() are given an alias of Member Name. Notice also that the<br />

alias is enclosed in single quotes because there are spaces in the alias. The same is true if you want to use<br />

characters such as punctuation in the alias.<br />

In the preceding example, you can see that spaces are added to ensure that the sentence reads correctly.<br />

A space should appear between a member’s first name and a member’s last name: “The member is<br />

called Katie Smith,” not “The member is called KatieSmith.”<br />

The second concatenation option provided by My<strong>SQL</strong> is the CONCAT_WS() function, which adds a separator<br />

between each of the columns or literals to be concatenated. If you want a single space between each<br />

column, you could write a query similar to the following:<br />

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

FROM MemberDetails;<br />

Executing the query with the CONCAT_WS() function provides exactly the same results as the previous<br />

example:<br />

Member Name<br />

The member is called Katie Smith<br />

The member is called Steve Gee<br />

The member is called John Jones<br />

The member is called Jenny Jones<br />

The member is called John Jackson<br />

Extracting Information<br />

Table continued on following page<br />

89

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

Saved successfully!

Ooh no, something went wrong!