Beginning SQL

Beginning SQL Beginning SQL

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

If you put square brackets around it (as shown below), the database system is happy: SELECT DateOfBirth AS [One***Two] FROM MemberDetails; That covers concatenation in MS Access and SQL Server; now it’s time to look at Oracle and DB2’s way of concatenating. Oracle and IBM DB2 There are two ways of concatenating text data or text-based columns in Oracle and DB2. The first is to use the concatenation operator, which in these systems is two vertical pipe (||) characters (see the following statement). The second is to use the CONCAT() function, which is covered later in this section. SELECT FirstName || ‘ ‘ || LastName AS FullName FROM MemberDetails; The preceding SQL returns the following results: FullName Katie Smith Susie Simons John Jackson Steve Gee John Jones Jenny Jones Jack Johnson Seymour Botts Jamie Hills Stuart Dales William Doors Doris Night As with SQL Server, not only can you join columns, but you can also join text. In the preceding example, you added a space between the first name and last name to improve readability; otherwise, results would have been KatieSmith, SandraTell, and so on. When you join columns, you can add whatever text you wish, as shown in the following statement: SELECT ‘First name is ‘ || FirstName || ‘, last name is ‘ || LastName FullName FROM MemberDetails; The preceding code produces these results: Extracting Information 85

If you put square brackets around it (as shown below), the database system is happy:<br />

SELECT DateOfBirth AS [One***Two] FROM MemberDetails;<br />

That covers concatenation in MS Access and <strong>SQL</strong> Server; now it’s time to look at Oracle and DB2’s way<br />

of concatenating.<br />

Oracle and IBM DB2<br />

There are two ways of concatenating text data or text-based columns in Oracle and DB2. The first is to<br />

use the concatenation operator, which in these systems is two vertical pipe (||) characters (see the following<br />

statement). The second is to use the CONCAT() function, which is covered later in this section.<br />

SELECT FirstName || ‘ ‘ || LastName AS FullName FROM MemberDetails;<br />

The preceding <strong>SQL</strong> returns the following results:<br />

FullName<br />

Katie Smith<br />

Susie Simons<br />

John Jackson<br />

Steve Gee<br />

John Jones<br />

Jenny Jones<br />

Jack Johnson<br />

Seymour Botts<br />

Jamie Hills<br />

Stuart Dales<br />

William Doors<br />

Doris Night<br />

As with <strong>SQL</strong> Server, not only can you join columns, but you can also join text. In the preceding example,<br />

you added a space between the first name and last name to improve readability; otherwise, results<br />

would have been KatieSmith, SandraTell, and so on. When you join columns, you can add whatever text<br />

you wish, as shown in the following statement:<br />

SELECT ‘First name is ‘ || FirstName || ‘, last name is ‘ || LastName FullName<br />

FROM MemberDetails;<br />

The preceding code produces these results:<br />

Extracting Information<br />

85

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

Saved successfully!

Ooh no, something went wrong!