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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 select random between 2 numbers
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Constraint Violating Yak Guru

291 Posts

Posted - 02/07/2013 :  08:08:02  Show Profile  Reply with Quote
I need to insert into a field for testing, but the field has a contraint on an ID from abother field. I want to do un update with a random number between 2 defined numbers like 21 and 61??

Helixpoint Web Development

Constraint Violating Yak Guru

291 Posts

Posted - 02/07/2013 :  08:38:58  Show Profile  Reply with Quote
Got it
Declare @maxRandomValue tinyint = 15 , @minRandomValue tinyint = 29;

UPDATE tblTrkRecord
SET SubStatusId =
(SELECT CAST((@maxRandomValue + 1 - @minRandomValue) * RAND() + @minRandomValue AS tinyint) AS Expr1)
WHERE (StatusId = 7)

Helixpoint Web Development
Go to Top of Page

Flowing Fount of Yak Knowledge

4614 Posts

Posted - 02/07/2013 :  12:32:40  Show Profile  Reply with Quote
You might also look into using the NEWID() function for generating random values or selecting random rows as RAND() is not very reliable in multi-row scenarios.

Populating a SQL Server Test Database with Random Data:

Retrieving Random Rows from Table Using NEWID():
Go to Top of Page

Premature Yak Congratulator

22864 Posts

Posted - 02/08/2013 :  07:35:14  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
You may also be interested to read this too


Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000