Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Generating Random Numbers on Every Row with RAND()

Author  Topic 

Rock_query
Yak Posting Veteran

55 Posts

Posted - 2013-05-01 : 17:26:16
I am using the RAND() function. I am multiplying by 20 to get a larger number since RAND returns numbers between 0 and 1.

The problem is that RAND generates only 1 random number and copies that down each row. But I want a different randome number on each row.

I am using the AdventureWorks2012 database on SQL Server Express on my laptop.


DECLARE @Drago TABLE (
ProductID int IDENTITY (1,1),
Price money NOT NULL,
Qty int NULL
)


INSERT INTO @Drago (
Price,
Qty
)

SELECT ListPrice, RAND()*20
FROM Production.Product

SELECT *
FROM @Drago


Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-05-01 : 17:43:10
Don't use the RAND function. Try using the NEWID function.

http://stackoverflow.com/questions/2247554/random-number-on-sql-without-using-newid
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-02 : 04:36:10
http://beyondrelational.com/modules/2/blogs/70/posts/19431/rand-function-reset-values-for-each-group.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -