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

Top 5 Contributors of the Month

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

on what basis NEWID() keyword in sql generate random numbers

Posted By: jay     Posted Date: March 17, 2010    Points:2   Category :Sql Server
on what basis NEWID() keyword in sql generate random numbers. whats the logic behind it.

Author: Naresh             
Posted Date: March 17, 2010     Points: 5   


This is the code Random Number Generation,

public string ticketno()
StringBuilder builder = new StringBuilder("IBM-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);

this.TextBox1.Text = ticketno();

The OutpUt will be,

TextBox =IBM-00045556


Author: Syed Shakeer Hussain             
Posted Date: March 17, 2010     Points: 5   

Hi jay ,
To genarate Random Number using NEWID() write the following code:

Run the below query in Sql Query Analyzer you will get Random Number when you run the query number of time

select ABS(CAST(CAST(NEWID() as VARBINARY)as INT))as RaondumNumber

or to use with the table,run the below query
select Top 5 ABS(CAST(CAST(NEWID() as VARBINARY)as INT))as RaondumNumber,* from table_name

Dont for forget to make it as Answer

Author: jay             
Posted Date: March 17, 2010     Points: 5   

thanks malathy .... one more question whats the logic(CODE) behind the inbuilt function Random().

Author: jay             
Posted Date: March 31, 2010     Points: 5   

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

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