.NET Answers

ASP.NET, HTML, CSS, Visual Studio, CSharp, VB.NET and other programming items of interest.
Subscribe
  • Home
  • About Me
  • Advertising
  • Click Here to Ask a question
    • Privacy Policy
  • Site Map

Random in SQL – SQL For Programmers

November 04, 2008 By: Dave

IMG_1382 Last week I had an interesting assignment: Given a specific record, randomly retrieve three related records from the database.

Naturally, as a programmer, I started looking for the SQL random function.  Which I found.

But even as I was searching for how to use that function, I was thinking to myself, “How am I going to structure things in such a way so as to randomly select the records once I have the function?  It isn’t like I can retrieve the records into an array and then select them out.  Am I going to have to retrieve them into a temp table?”

And that’s when I stumbled onto a slick little trick.  The NewID() function.

The NewID() function returns a value that looks something like a GUID.  The beauty of this function is that when you use it as a column in your return set and set the order to that column, you end up with a randomly sorted list of records.

So what I thought was going to be lines and lines of SQL code ended up being a rather simple select statement:

SELECT TOP 3 field1, field2, NewId as OrderId
    FROM databaseTable
    WHERE @filter = filter
    ORDER BY OrderId

Other places talking about NewId():

Generate a unique number in Sql Server 2005 – Let us quickly take a tour of how to do that using Sql server 2005. SQL Server contains the NEWID() function. This function creates a unique value of type uniqueidentifier. We can use this function in several ways to generate unique …

Identity vs uniqueidentifier – You have to use “NEWID()” SQL function to create a new GUID and then assign it to the column which is uniqueidentifier type. uniqueidentifier type is good since GUID’s are unique in space and time. So even if you merge two tables with …

Generating Random Data for SQL Server and MySQL – SQL Server – uses NEWID() in ORDER BY. 1: — using the AdventureWorks Database 2: SELECT 3: TOP 10 4: DepartmentID, 5: [Name] 6: FROM 7: HumanResources.Department 8: ORDER BY 9: NEWID() 10:. MySQL – uses RAND() in ORDER BY. 1: SELECT

 

Other post in SQL For Programmers
  • SQL For Developers - 9 Reasons to bother - August 13th, 2008
  • SQL for Programmers - CREATE/DROP Database - August 20th, 2008
  • SQL For Programmers - CREATE TABLE - August 22nd, 2008
  • SQL For Programmers - ALTERing the TABLE - September 1st, 2008
  • SQL For Programmers - Finding a String - September 3rd, 2008
  • SQL For Programmers - Finding IN a List - September 8th, 2008
  • SQL For Programmers - Stored Procedures (Better than LINQ) - September 10th, 2008
  • SQL For Programmers - Stored Procedure Basics - September 16th, 2008
  • Basic SQL Commands - SQL For Programmers - September 18th, 2008
  • SQL IF/WHILE Blocks - SQL For Programmers - September 30th, 2008
  • SQL WHILE - SQL For Programmers - October 2nd, 2008
  • Temporary Tables - SQL For Programmers - October 7th, 2008
  • SQL CURSOR - SQL For Programmers - October 13th, 2008
  • SQL CURSOR Performance - SQL For Programmers - October 22nd, 2008
  • Random in SQL - SQL For Programmers - November 4th, 2008
  • SQL - Filtering WHERE condition on two rows - November 26th, 2008
  • SQL - Transactions - April 15th, 2009

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Related Post

  • SQL – Filtering WHERE condition on two rows
  • SQL WHILE – SQL For Programmers
  • Basic SQL Commands – SQL For Programmers
Bookmark to:

Add to Del.icio.us Add to digg Add to DotNetKicks Add to DZone Add to Facebook Add to Slashdot Add to Stumble Upon Add to Technorati
Hide Sites
Tags: ms-sql, random, sql, tsql

2 Responses to “ Random in SQL – SQL For Programmers ”

  1. # 1 Dew Drop - November 4, 2008 | Alvin Ashcraft's Morning Dew Says:
    November 4th, 2008 at 8:10 am

    [...] Random in SQL – SQL For Programmers (Dave M. Bush) [...]

  2. # 2 SQL Server Stuff « QuantuMatrix’s Weblog Says:
    April 16th, 2009 at 12:06 am

    [...] Random in SQL – SQL For Programmers – November 4th, 2008 [...]

← CSharp Enum
DotNetNuke Modules – Automating the PA →
  • Search

  • Subscribe

    U COMMENT
    I FOLLOW

    Subscribe in a reader

    OR

    Subscribe via e-mail

    Enter your email address: 

    Delivered by FeedBurner

     

  • Follow Me

    • Twitter
    • FaceBook
    • Digg
    • StumbleUpon
    • Propeller
    • Delicious
    • Plaxo

     

  • Recent Posts

    • ASUS Eee PC 1005HA-PU1X-BK Black Netbook
    • jQuery – Date Picker
    • Using VB.NET From CSharp
    • iTextSharp – Adding Images
    • Hungarian Notation – Use What Works, Spit Out The Bones
    • Pre Order Windows 7
    • jQuery Dialog – With Validation Controls
    • iTextSharp – The easy way
    • Structure of my ASP.NET Web Applications
    • 35% Off Accronis True Image 2009 Home
    • VB.NET Hide Module Name
    • ASP.NET/VB.NET – Video Training
    • Does jQuery Make Us Lazy?
    • PDFs Using iTextSharp
    • Programming SEO – Ping



  • Advertise on this site through Lake Quincy Media
  • DotNetNuke Sponsor

     

    Most Valuable Blogger
  • Sponsor

  • Categories

    • Advanced CSharp
    • Advanced VB.NET
    • ASP.NET MVC
    • Did you know
    • DotNetNuke – Module Development
    • DotNetNuke – Skinning
    • internationalization
    • iTextSharp
    • jQuery
    • none
    • Seach Engine Optimization
    • Silverlight
    • SQL For Programmers
    • Twitter
    • winforms
  • Cloud

    .net ajax architecture asp.net book books containers csharp css dal dataset datasets dotnetnuke events gridview images internationalization internet explorer javascript jQuery json linq listview modules ms-sql MVC objectdatasource programming reflection seo Silverlight skinning sql testing tsql tutorial Twitter twitterizer vb.net video view Vista visual studio webservice WordPress
  • Archives

    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
    • Privacy Policy
  • Calendar

    November 2008
    S M T W T F S
    « Oct   Dec »
     1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30  
  • Blogroll

    • Alvin Ashcraft’s Morning Dew
    • ASP.NET Consulting
    • Life Hacker
    • Remember Anything
    • The Price of Their Toys
    • Uncategorized Thought


.NET Answers © 2007 - 2008 All Rights Reserved.
Entries and Comments.