selecting rows randomly from a table or database

  • Hi,

    I am trying to select rows randomly from a table. TOP n [percent] only gives the top %age of rows. is there any way i can do it using T-SQL?

  • Not in T-SQL. You would have to do this with some fancy scripting.

    Steve Jones

    steve@dkranch.net

  • Not really fancy but you could try something like this

    DECLARE @var AS INT

    SELECT @var = RAND( (DATEPART(mm, GETDATE()) * 100000 )

    + (DATEPART(ss, GETDATE()) * 1000 )

    + DATEPART(ms, GETDATE()) )

    /* You will need to decide how Rand can best help you as in this way it may not be a large enough number. This should give you a blueprint though to what may help you. */

    SELECT columnlist FROM tblX outX WHERE (@var-1) = (SELECT COUNT(*) FROM tblX inX WHERE inX.uniid > outX.uniid

    "Don't roll your eyes at me. I will tape them in place." (Teacher on Boston Public)

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply