| TECHNICAL
ARTICLES ASP ASP.NET JavaScript Transact SQL Other Articles Software Reviews PHOTO GALLERIES TRAVEL LOG MORE STUFF POPULAR STUFF |
Home > Articles > Transact SQL Programming Articles Generating Random Numbers in Transact-SQLThe Transact SQL Rand function can be used to return a random floating point number between 0 and 1: SELECT RAND() AS RandomNumber The Rand function can also be supplied with an integer value (i.e. smallint, tinyint or int) to use as a random seed: SELECT RAND(@MyInteger) AS RandomNumber Creating Random Numbers in a Certain Numerical RangeThe following SQL code can be used to generate random integers between the values of the @MinValue and @MaxValue variables. DECLARE @RandomNumber float The output of this SQL will be a random number between 2 and 12 (inclusive). Random Numbers in SELECT StatementsAn important consideration is that if the RAND function is called within a single query then it will return the same random number. You might, therefore, want to consider a different approach, such as the solution described in the article generating randomly ordered SQL Server result sets. Useful Links
|
| Site Map | Privacy Policy | All content is © 1995 - 2012 Brett B |