Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 20th, 2003, 05:08 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by planoie
 I learned this from my buddy Imar just this morning!

Try:
SELECT TOP 10 * FROM ORArticles WHERE ISNULL(Photo1, '') <> '' ORDER NEWID()

This generates a new GUID for every row and orders by that. Because GUIDs are generated in, for all intents and purposes, a random way, it works very well for "randomizing" a set or rows.

Peter
------------------------------------------------------
Work smarter, not harder.
This works in any implementation which does not require that the ORDER BY column be present in the SELECT column list (like SQL Server).

The OP wanted just one from the top 10 list, as I read his post:
Code:
SELECT TOP 1 * FROM (SELECT TOP 10 * ORArticles WHERE COALESCE(Photo1, '') <> '' ORDER BY NewID()) as T1
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old November 20th, 2003, 05:38 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Now that I look at this one more time, I realized that Jeff and I were both missing something...

I think what morpheus might want is a random row out of the top ten of your table based on the MagID, not just 1 random row from all of them. So we must add in the MagID ordering...

SELECT TOP 1 *
FROM (SELECT TOP 10 *
        FROM ORArticles
        WHERE ISNULL(Photo1, '') <> ''
        ORDER by MagID DESC) AS TopTen
ORDER BY NEWID()

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old November 20th, 2003, 05:46 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

You are quite correct.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Null Value Yasho VB.NET 2002/2003 Basics 1 June 21st, 2007 04:37 AM
Null Ignored... Why? SerranoG Access VBA 13 December 28th, 2005 12:41 PM
How to set Not Null constraint to Null Columns arasu Oracle 1 August 22nd, 2005 10:09 AM
Is A Null Different from Another Null ramk_1978 SQL Language 9 February 12th, 2005 03:18 PM
when to use is Null and =null and"null shoakat Classic ASP Databases 3 October 29th, 2004 01:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.