Choose Random Values

Rajnilari2015
Posted by Rajnilari2015 under Sql Server category on | Points: 40 | Views : 230
Let's say we have a table tblRecords with with key(int, PK) and Value(varchar).The values being populate are

Key         Value
---- ------------
1 Value1
2 Value2
3 Value3
4 Value4
5 Value5
6 Value6
7 Value7
8 Value8
9 Value9
10 Value10


We need to choose randomly the keys from 1 to 10. The below is the code to do so

SELECT * 
FROM dbo.tblRecords
WHERE [Key] = CAST(RAND()*10 AS INT)

Comments or Responses

Login to post response