Beginning SQL
Beginning SQL Beginning SQL
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
- Page 336: Chapter 4 148 UPDATE Films SET Film
- Page 340: Chapter 4 150 Finally, you do the s
- Page 344: Chapter 4 152 SELECT * FROM MemberD
- Page 348: Chapter 4 ❑ Choose meaningful nam
- Page 354: 5 Manipulating Data In Chapter 3, y
- Page 358: The results of this query are as fo
- Page 362: You can raise a number to any power
- Page 366: To demonstrate the sort of results
- Page 370: The CEILING() Function The CEILING(
- Page 374: ased on the digits after the decima
- Page 378: The SUBSTRING() function works at t
- Page 382: LastName UPPER(LastName) LOWER(Last
- Page 388: Chapter 5 174 In Oracle and IBM’s
- Page 392: Chapter 5 176 FirstName SOUNDEX(Fir
- Page 396: Chapter 5 Date Functions 178 Someti
- Page 400: Chapter 5 The syntax for the CAST()
- Page 404: Chapter 5 NULLs and Strings 182 NUL
- Page 408: Chapter 5 184 SELECT FirstName, Las
- Page 412: Chapter 5 186 This SQL copies over
- Page 416: Chapter 5 You also learned that mat
- Page 420: Chapter 6 190 Begin by looking at h
- Page 424: Chapter 6 This section covers queri
- Page 428: Chapter 6 194 This query gives the
- Page 432: Chapter 6 You’re almost there. Yo
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