Forum Moderators: open

Message Too Old, No Replies

SQL Randomize Code

need one line sql code link RND

         

abulhallaj

6:12 pm on Jan 29, 2005 (gmt 0)

10+ Year Member



Hi to all

I need to select a random record off data with one line query. it's need to be used inside ASP page written by VBScript.

Thanks

bateman_ap

6:17 pm on Jan 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use a select statement returning only one record and at the end use "ORDER BY NewID()"

ie


SELECT TOP 1 ID FROM YOUR_TABLE ORDER BY NEWID()

abulhallaj

7:59 am on Jan 31, 2005 (gmt 0)

10+ Year Member



My database is access and I use Microsoft.Jet.4 for connection to db ,SO NewID() function don't work . I think NewID()is only work on mysql.

I need a code that work on Access DB
I used the below code too:

i = int(rnd*100)+1
strSQL = "SELECT TOP 1 * FROM tbl ORDER BY RND(" & i & ");"

but it alwayes return the same record

mattglet

1:02 pm on Jan 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:

Randomize()
i = int(rnd*100)+1
strSQL = "SELECT TOP 1 * FROM tbl ORDER BY RND(" & i & ");"

Randomize is a built-in VBScript function.

Spooky

11:49 pm on Feb 2, 2005 (gmt 0)

10+ Year Member



Newid() also works on the latest jet 4 drivers

abulhallaj

8:48 am on Feb 8, 2005 (gmt 0)

10+ Year Member



I used all of this method but they don't work!

:(( :(( :(( :(( - :(( :(( :(( :((

mattglet

10:28 pm on Feb 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you getting errors, or are you continuing to have the same number show up?

CaseyRyan

7:20 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



How many records are in your table? This may also be a factor in what is showing up. If you don't have a lot, probabilty is higher you'll recieve the same record.

-=casey=-

abulhallaj

5:42 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



i have the same number for all records