.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Sql Server >> Post New QuestionBookmark and Share Subscribe to Forum

Generate Random number in SQL Server

Posted By: Shweta     Posted Date: February 12, 2011    Points:2   Category :Sql Server
How to generate random number in sql server and save in in a column?

Thanks In Advance
Shweta

Responses
Author: Pandian          Accepted Answer   
Posted Date: February 12, 2011     Points: 10   

Hi

Yes. you can generate it..

SELECT ROUND(RAND() * 100,0)
GO

- The random number between 0 through 100

Cheers
www.sqlserverbuddy.blogspot.com


If not I then who, If not now then when
Author: shankey             
Posted Date: February 14, 2011     Points: 5   

hi,

Insert into RandomTable(randomnumber)
SELECT ROUND(RAND() * 1000,0)

Generates number from 0,1000 and insert in a table

Shankey
Author: Naresh             
Posted Date: February 15, 2011     Points: 5   

hi,,

The code for random no generation

// To generate the Random Number automatically.
public string ticketno()
{
StringBuilder builder = new StringBuilder("000");
builder.Append(randomno(1, 99999));
return builder.ToString();
}
private int randomno(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}

//Call the function in textbox.

this.TextBox1.Text = ticketno();


Thanks
Naresh
Author: Narayanan             
Posted Date: July 20, 2011     Points: 20   
Author: Ravi Ranjan Kumar             
Posted Date: July 20, 2011     Points: 20   

hey! have a go at given links where you can find a SQL script which will help you to generate Random Numbers
http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/

Thanks & Regards
Ravi Ranjan Kumar
http://raviranjankr.wordpress.com
Author: Ravi Ranjan Kumar             
Posted Date: July 20, 2011     Points: 20   

In addition to my previous answer You can also try
SET @Lower = 1000 - The lowest random number
SET @Upper = 9999 - The highest random number
SELECT @Random = Round(((@Upper - @Lower -1) * Rand() + @Lower), 0)
SELECT @Random


Thanks & Regards
Ravi Ranjan Kumar
http://raviranjankr.wordpress.com


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend