SQL Server has a built in random function that can be used to produce random
float numbers
between 0 and 1.
Select RAND( ), RAND( ), RAND( );
0.99606277761155 |
0.258347648718297 |
0.198726256206626 |
When invoked with an integer argument,
RAND( ) uses that value to seed the random number generator.
Each time you seed the generator with a given value,
RAND( ) will produce a repeatable series of numbers:
SELECT RAND(1), RAND( ), RAND( );
0.713591993212924 |
0.472241415009636 |
0.943996112912269 |
You can use
ORDER BY RAND() to randomize a set of rows or values as follows:
To understand
ORDER BY RAND() function consider an
Products table which is having following records:
SELECT * FROM Products;
Prd001 |
Pen |
40.25 |
Prd002 |
Pen |
100.75 |
Prd003 |
Eraser |
10.35 |
Prd004 |
Pencil |
20.45 |
Prd005 |
Book |
50.65 |
Prd006 |
Gum |
25.22 |
Now use the following commands:
SELECT * FROM Products ORDER BY RAND();
Prd001 |
Pen |
40.25 |
Prd002 |
Pen |
100.75 |
Prd003 |
Eraser |
10.35 |
Prd004 |
Pencil |
20.45 |
Prd005 |
Book |
50.65 |
Prd006 |
Gum |
25.22 |