selecting random records

Last post 05-12-2008 1:19 AM by smoothmovz. 3 replies.

Sort Posts:

  • selecting random records

    05-11-2008, 10:38 PM
    • Loading...
    • smoothmovz
    • Joined on 04-17-2008, 4:56 AM
    • Philippines
    • Posts 33

    hi again,

    i just want to ask if how can i randomly select 5 distinct records from a table w/ a hundreds of records everytime i exec a stored procedure??

    thanks Smile

  • Re: selecting random records

    05-11-2008, 11:08 PM
    Answer
    • Loading...
    • limno
    • Joined on 06-10-2005, 3:50 PM
    • Iowa, USA
    • Posts 2,803
    • Moderator
      TrustedFriends-MVPs

    Try this:

    SELECT TOP (5) * FROM authors ORDER BY newid()

    Limno

  • Re: selecting random records

    05-12-2008, 1:09 AM
    Answer
    • Loading...
    • eralper
    • Joined on 10-11-2002, 4:26 AM
    • Turkey
    • Posts 193

    Hello smoothmovz,

    You can add the CheckSum() encapsulating the NEWID() function in the orer by clause 

    DECLARE @n INT

    SET @n = 5

    SELECT TOP (@n) * FROM Brands ORDER BY CHECKSUM(NEWID())

     

    Eralper

    Select top n records or random n records for each category

     

    http://www.eralper.com
    http://www.kodyaz.com
  • Re: selecting random records

    05-12-2008, 1:19 AM
    • Loading...
    • smoothmovz
    • Joined on 04-17-2008, 4:56 AM
    • Philippines
    • Posts 33

    thanks for your helpful replies, can you help me with my other problem. here's my post:

    http://forums.asp.net/p/1259677/2351246.aspx#2351246

Page 1 of 1 (4 items)