20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

FirstName DIFFERENCE(FirstName, ‘Katherine’) DIFFERENCE(FirstName, ‘Johnny’)<br />

John 1 4<br />

Jack 0 3<br />

John 1 4<br />

Jamie 1 4<br />

Catherine 3 1<br />

Steve 1 1<br />

William 1 2<br />

Stuart 2 0<br />

Seymour 1 2<br />

From the results, you can see that a difference value of 3 or 4 means that the two strings sound either<br />

identical or quite similar. For example, a comparison of John and Johnny gives a difference value of 4,<br />

but a comparison of Johnny and Stuart gives a value of 0.<br />

Using the DIFFERENCE() function in the WHERE clause makes more sense, because doing so limits<br />

results to just those names that sound the most similar. Consider the following query:<br />

SELECT FirstName<br />

FROM MemberDetails<br />

WHERE<br />

DIFFERENCE(FirstName, ‘Katherine’) >= 3 OR DIFFERENCE(FirstName, ‘Johnny’) >= 3<br />

The preceding code compares the value returned by DIFFERENCE() functions, if the value returned is<br />

greater than or equal to 3, with the assumption that the names are reasonably similar.<br />

On <strong>SQL</strong> Server, the query provides the following results:<br />

FirstName<br />

Jenny<br />

John<br />

Jack<br />

John<br />

Jamie<br />

Catherine<br />

Manipulating Data<br />

The results have returned names that are similar, or at least vaguely similar, to either Johnny or<br />

Katherine. For example, Katherine is very similar to Catherine. All these results are due to the<br />

comparison made by the DIFFERENCE() function returning a value of 3 or more. You may get different<br />

results depending on which database system you use.<br />

177

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

Saved successfully!

Ooh no, something went wrong!