Fetch random records from the table using NewID() function

Query: Am having 10000 records and I need some 2000 random records. How can I fetch the same? Answer:



This can be acheived using NEWID() function. NewID() will try to generate a unique id of 32 bit length. (Ex value A1E7AB10-3E0C-4685-9FEE-00239FB80F4F)
 
 
---------Am creating a sample table and insert the records into the table
 
DROP TABLE VENKAT_TABLE
 
GO
 
CREATE TABLE VENKAT_TABLE (ID INT IDENTITY(0,1))
 
GO
 
 INSERT INTO VENKAT_TABLE DEFAULT VALUES
 
GO 1000
 
------- Inserting 1000 records into the table
 
SELECT * FROM VENKAT_TABLE



--------- Query to fetch random 100 records from the table.
SELECT TOP 100 * FROM VENKAT_TABLE ORDER BY NEWID()
 


Cheers,
 Venkatesan Prabu .J
 
http://kaashivinfotech.com/