SQL Server generates a random number rand function

Source: Internet
Author: User
Tags float number rand

https://docs.microsoft.com/en-us/sql/t-sql/functions/rand-transact-sql?view=sql-server-2017

Generate random numbers within a certain interval

Https://stackoverflow.com/questions/7878287/generate-random-int-value-from-3-to-6

Principle, the RAND function generates a random float number between 0 and 1.

With this random number * interval range + minimum, a random number within an interval range is obtained. Then use the round function to remove the digits after the decimal point.

DECLARE @Random INT;DECLARE @Upper INT;DECLARE @Lower INTSET @Lower = 3 ----the lowest random numberSET @Upper = 7 ----one more than the highest random numberSELECT @Random = ROUND(((@Upper - @Lower -1)* RAND()+ @Lower),0)SELECT @Random

Generating a Boolean value

https://stackoverflow.com/questions/20597269/how-to-generate-random-boolean-value-in-sql-server-2008

If you is only generating one row, you could use something as simple as:

SELECT CAST (ROUND(RAND(),0asBIT)

However, if you is generating more than one row, would evaluate to the RAND() same value for every row, so please see Marti n Smith ' s answer.

SQL Server generates a random number rand function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.