datetime century date into pad dynamic cursor money percent sp job isnumeric isdate over update
SQLUSA.com
SQL 2008 GRAND SLAM ON 49 CD
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL Server Training SQL 2005 Scripts SQL 2008 Articles
SQL JOBS News Format Developer
How to get random results from SELECT?

Execute the Microsoft SQL Server T-SQL code example to demonstrate random sampling of the Product table:

-- T-SQL random sampling with NEWID and TOP functions

SELECT TOP (7)

       ProductID, ProductName=Name, ProductNumber,

       Color, ListPrice

FROM AdventureWorks2008.Production.Product

WHERE ListPrice > 0 and Color is not null

ORDER BY NEWID()

/*

PID   ProductName                   ProdNo      Color       ListPrice

849   Men's Sports Shorts, M        SH-M897-M   Black       59.99

792   Road-250 Red, 58              BK-R89R-58  Red         2443.35

828   HL Road Rear Wheel            RW-R820     Black       357.06

884   Short-Sleeve Jersey,    XL    SJ-0194-X   Yellow      53.99

840   HL Road Frame - Black, 52     FR-R92B-52  Black       1431.50

720   HL Road Frame - Red, 52       FR-R92R-52  Red         1431.50

799   Road-550-W Yellow, 42         BK-R64Y-42  Yellow      1120.49

*/

 

------------

-- Generating Random Numbers with NEWID() & CHECKSUM()

------------

SELECT Random=CONVERT(VARCHAR,ABS(CHECKSUM(NEWID())))

FROM master.dbo.spt_values

ORDER BY Random;

/*

Random

1000945892

1003905302

1008347353  ..... */

------------

Related article:

NEWID (Transact-SQL)

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
Microsoft SQL Server 2012 Training Videos at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Accounting
Administrative
Advertising
Arts
Architecture
Banking
Business Intelligence
Career Jobs
Celebrity
Computer
Consulting
Customer Service
Education
Engineering
Entertainment
Entry Level
Executive
Federal
Finance
Government
Hardware
Healthcare
Hospital
Human Resources
Information Technology
Insurance
Internet
Job Openings
Laboratory
Law Enforcement
Legal
Logistics
Manufacturing
Marketing
Medical
Military
Nursing
Pharmaceutical
Physician
Public Relations
Publishing
Real Estate
Restaurant
Retail
Sales
Social Media
Software
SQL Database
Telecomm
Therapist
Training
Transportation
Truck Driver
Travel
Web
Work from Home

FREE SS SQL / BI OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011 Microsoft Community Contributor 2012

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts

JOIN US ON TWITTER

Copyright 2005-2012, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.