Beginning SQL

Beginning SQL Beginning SQL

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

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

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

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

Saved successfully!

Ooh no, something went wrong!