|
INTERVIEW QUESTIONS
DATABASE
MS SQL SERVER
DETAILS
Question: How To Generate Random Numbers with the RAND() Function in MS SQL Server?
Answer: Random numbers are very useful for generating test data, passwords, or other security related data. SQL Server 2005 offers you the random number generator function RAND in two format:
* RAND(seed) - Starting a new sequence of random numbers based on the given integer "seed" and returning the first random number in FLOAT(53) from the sequence. * RAND() - Returning the next random number in FLOAT(53) from the current sequence. If there has been no current sequence, SQL Server will start a new sequence with a random "seed".
Note that calling RAND(seed) with the same seed will start the same sequence and return the same number. To avoid this repeating pattern, you should always call RAND() without any seed and let the server to randomly pickup a sequence. The tutorial exercise below shows some good examples on how to generate random numbers:
SELECT RAND(100), RAND(), RAND(); -- new sequence SELECT RAND(100), RAND(), RAND(); -- same sequence again SELECT RAND(), RAND(), RAND(); SELECT RAND(), RAND(), RAND(); GO 0.715436657367485 0.28463380767982 0.0131039082850364 0.715436657367485 0.28463380767982 0.0131039082850364 0.28769876521071 0.100505471175005 0.292787286982702 0.868829058415689 0.370366365964781 0.58334760467751
|
|
|
Category |
MS SQL Server Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 8405 users |
Added on |
9/23/2014 |
Views |
68044 |
Rate it! |
|
|
Question:
How To Generate Random Numbers with the RAND() Function in MS SQL Server?
Answer:
Random numbers are very useful for generating test data, passwords, or other security related data. SQL Server 2005 offers you the random number generator function RAND in two format:
* RAND(seed) - Starting a new sequence of random numbers based on the given integer "seed" and returning the first random number in FLOAT(53) from the sequence. * RAND() - Returning the next random number in FLOAT(53) from the current sequence. If there has been no current sequence, SQL Server will start a new sequence with a random "seed".
Note that calling RAND(seed) with the same seed will start the same sequence and return the same number. To avoid this repeating pattern, you should always call RAND() without any seed and let the server to randomly pickup a sequence. The tutorial exercise below shows some good examples on how to generate random numbers:
SELECT RAND(100), RAND(), RAND(); -- new sequence SELECT RAND(100), RAND(), RAND(); -- same sequence again SELECT RAND(), RAND(), RAND(); SELECT RAND(), RAND(), RAND(); GO 0.715436657367485 0.28463380767982 0.0131039082850364 0.715436657367485 0.28463380767982 0.0131039082850364 0.28769876521071 0.100505471175005 0.292787286982702 0.868829058415689 0.370366365964781 0.58334760467751 Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
How To Round a Numeric Value To a Specific Precision?
|
View Answer
|
|
How To Convert Numeric Values to Integers in MS SQL Server?
|
View Answer
|
|
What Are the Mathematical Functions Supported by SQL Server 2005?
|
View Answer
|
|
What Happens When Converting Big Values to NUMERIC Data Types?
|
View Answer
|
|
What Happens When Converting Big Values to Integers?
|
View Answer
|
|
How To Convert Character Strings into Numeric Values?
|
View Answer
|
|
How To How To Convert Numeric Expression Data Types using the CONVERT() Function??
|
View Answer
|
|
How To Convert Numeric Expression Data Types using the CAST() Function?
|
View Answer
|
|
How To Convert Numeric Expression Data Types by Assignment Operations?
|
View Answer
|
|
How To Convert a Numeric Expression from One Data Type to Another?
|
View Answer
|
|
What Is an Expression in MS SQL Server?
|
View Answer
|
|
What Are the Underflow and Overflow Behaviors on FLOAT Literals?
|
View Answer
|
|
How REAL and FLOAT Literal Values Are Rounded?
|
View Answer
|
|
How Extra Digits Are Handled with NUMERIC Data Type Literals?
|
View Answer
|
|
What Happens If an Integer Is Too Big for INT Date Type?
|
View Answer
|
|
What Are Out-of-Range Errors with Date and Time Literals?
|
View Answer
|
|
What Happens If Time-Only Values Are Provided as Date and Time Literals?
|
View Answer
|
|
What Happens If Date-Only Values Are Provided as Date and Time Literals?
|
View Answer
|
|
Why I Can Not Enter 0.001 Second in Date and Time Literals in MS SQL Server?
|
View Answer
|
|
How To Enter Date and Time Literals in MS SQL Server?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All MS SQL Server Interview Questions & Answers - Exam Mode /
Learning Mode
|