>SQL Server: Which is the Best Way to Generate Random Number


>

Following are two commonly used methods to generate random numbers. My question is that which one is better and why? I will post your answer on this blog with due credit.
Method-1:
By using RAND () built-in function
DECLARE @n AS INT;
SET @n = 1000;
SELECT CAST(RAND()*@n AS INT) + 1
Method-2:
By using CHECKSUM built-in function
DECLARE @n AS INT;
SET @n = 1000;
SELECT 1+ABS(CHECKSUM(NEWID())) % @n
Advertisements

Posted on January 17, 2011, in Sql Server 2005, Sql Server 2008, TSQL Tips n Tricks. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: