Beginning SQL

Beginning SQL Beginning SQL

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

The LENGTH() function takes just one argument, the string for which the length is required. So to find out how long each member’s last name is, use the following query: SELECT LastName, LENGTH(LastName) FROM MemberDetails; Remember to use LEN(), and not LENGTH(), if your database system is MS Access or SQL Server. The query’s results are as follows: LastName LENGTH(LastName) Smith 5 Simons 6 Night 5 Jones 5 Jones 5 Johnson 7 Jackson 7 Hills 5 Hawthorn 8 Gee 3 Doors 5 Dales 5 Botts 5 The LENGTH() function is especially useful when used in combination with other functions. For example, you can use it to make sure that the members’ surnames start with a capital letter. As it happens, all the last names are correctly entered with capital letters, so you won’t see any difference. The following example demonstrates the code converting strings to lowercase by using the LOWER() function so that you can see the LENGTH() function actually working. In SQL Server, the code is as follows: SELECT LastName, LOWER(SUBSTRING(LastName,1,1)) + SUBSTRING(LastName,2,LEN(LastName) - 1) FROM MemberDetails; If you’re using MySQL, use the following code: SELECT LastName, CONCAT(LOWER(SUBSTRING(LastName,1,1)), SUBSTRING(LastName,2,LENGTH(LastName) - 1)) FROM MemberDetails; Manipulating Data 173

The LENGTH() function takes just one argument, the string for which the length is required. So to find<br />

out how long each member’s last name is, use the following query:<br />

SELECT LastName, LENGTH(LastName)<br />

FROM MemberDetails;<br />

Remember to use LEN(), and not LENGTH(), if your database system is MS Access or <strong>SQL</strong> Server.<br />

The query’s results are as follows:<br />

LastName LENGTH(LastName)<br />

Smith 5<br />

Simons 6<br />

Night 5<br />

Jones 5<br />

Jones 5<br />

Johnson 7<br />

Jackson 7<br />

Hills 5<br />

Hawthorn 8<br />

Gee 3<br />

Doors 5<br />

Dales 5<br />

Botts 5<br />

The LENGTH() function is especially useful when used in combination with other functions. For example,<br />

you can use it to make sure that the members’ surnames start with a capital letter. As it happens, all<br />

the last names are correctly entered with capital letters, so you won’t see any difference. The following<br />

example demonstrates the code converting strings to lowercase by using the LOWER() function so that<br />

you can see the LENGTH() function actually working. In <strong>SQL</strong> Server, the code is as follows:<br />

SELECT LastName, LOWER(SUBSTRING(LastName,1,1)) +<br />

SUBSTRING(LastName,2,LEN(LastName) - 1)<br />

FROM MemberDetails;<br />

If you’re using My<strong>SQL</strong>, use the following code:<br />

SELECT LastName, CONCAT(LOWER(SUBSTRING(LastName,1,1)),<br />

SUBSTRING(LastName,2,LENGTH(LastName) - 1))<br />

FROM MemberDetails;<br />

Manipulating Data<br />

173

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

Saved successfully!

Ooh no, something went wrong!