• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting random records

 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
How can i get randomly 20 of 600 records from MSSSQL with SELECT statement?
Is there an SQL way?
Thanks in advance
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34654
365
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kudret,
I haven't used MSSQL, but there are two approaches I can think of. You'll need to check whether they are supported.

1) If the database supports random number generation, do an "order by" on that function and then use ROWNUM to return the first 20 rows.
2) Generate 20 random #s in Java between 1 and 600. Then add a clause to the WHERE to only return ROWNUMs matching those.
 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply




[ April 06, 2007: Message edited by: Kudret Serin ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic