Beginning SQL
Beginning SQL Beginning SQL
To demonstrate the sort of results RAND() gives, execute the following SQL: SELECT MemberId, MemberId + RAND(), RAND() FROM MemberDetails ORDER BY MemberId; The code returns MemberId, MemberId plus a random number, and finally a random number that the database system generates on its own. The query produces 13 results, one for each record in the table: MemberId MemberId + RAND() RAND() 1 1.133239 0.142173 4 4.311148 0.129218 5 5.712648 0.175588 6 6.739993 0.173202 7 7.64603 0.710545 8 8.614636 0.941545 9 9.863819 0.494457 10 10.88083 0.920768 11 11.96136 0.044484 12 12.33835 0.558281 13 13.77636 0.206976 14 14.70579 0.908012 15 15.4227 0.389447 Because it’s random, your results will be different from the ones shown here. Note that in MS SQL Server, the RAND() function generates the fractional part, and it also generates only one random number for each RAND() used in a results set. Most databases (IBM’s DB2, for example) create a new random number for each record. Such long fractional numbers are not really that useful, but you can make them whole numbers by multiplying them and then using one of the rounding functions (discussed in the next section) to cut off the fractional part. Rounding Numbers Manipulating Data The final three math functions covered here are all related, the relationship being that they all remove all or part of the fractional part of a floating-point number. The difference among the three is how they decide to remove the part after the decimal point. The three rounding functions are CEILING(), FLOOR(), and ROUND(), each covered in its own section. Before continuing with the discussion of the rounding functions, you need to add some data to the Film Club database. In addition to keeping track of whether a film is available on DVD, the film club 163
- Page 316: Chapter 4 If you’re using IBM DB2
- Page 320: Chapter 4 140 index helps speed up
- Page 324: Chapter 4 142 The results you get m
- Page 328: Chapter 4 144 The results of the qu
- Page 332: Chapter 4 146 Remember, IBM DB2 doe
- 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 368: Chapter 5 164 chairperson now also
- Page 372: Chapter 5 The FLOOR() Function The
- Page 376: Chapter 5 DVDPrice ROUND(DVDPrice,1
- Page 380: Chapter 5 Note that, as with all th
- Page 384: Chapter 5 The TRIM() Functions Trim
- 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
To demonstrate the sort of results RAND() gives, execute the following <strong>SQL</strong>:<br />
SELECT MemberId, MemberId + RAND(), RAND() FROM MemberDetails<br />
ORDER BY MemberId;<br />
The code returns MemberId, MemberId plus a random number, and finally a random number that the<br />
database system generates on its own.<br />
The query produces 13 results, one for each record in the table:<br />
MemberId MemberId + RAND() RAND()<br />
1 1.133239 0.142173<br />
4 4.311148 0.129218<br />
5 5.712648 0.175588<br />
6 6.739993 0.173202<br />
7 7.64603 0.710545<br />
8 8.614636 0.941545<br />
9 9.863819 0.494457<br />
10 10.88083 0.920768<br />
11 11.96136 0.044484<br />
12 12.33835 0.558281<br />
13 13.77636 0.206976<br />
14 14.70579 0.908012<br />
15 15.4227 0.389447<br />
Because it’s random, your results will be different from the ones shown here. Note that in MS <strong>SQL</strong><br />
Server, the RAND() function generates the fractional part, and it also generates only one random number<br />
for each RAND() used in a results set. Most databases (IBM’s DB2, for example) create a new random<br />
number for each record. Such long fractional numbers are not really that useful, but you can make them<br />
whole numbers by multiplying them and then using one of the rounding functions (discussed in the<br />
next section) to cut off the fractional part.<br />
Rounding Numbers<br />
Manipulating Data<br />
The final three math functions covered here are all related, the relationship being that they all remove all<br />
or part of the fractional part of a floating-point number. The difference among the three is how they<br />
decide to remove the part after the decimal point. The three rounding functions are CEILING(), FLOOR(),<br />
and ROUND(), each covered in its own section.<br />
Before continuing with the discussion of the rounding functions, you need to add some data to the<br />
Film Club database. In addition to keeping track of whether a film is available on DVD, the film club<br />
163