SQL Server random number Function

Source: Internet
Author: User
Tags floor function

However, I still hope that the old birds will give me more advice. Now let's get started with the question:
Random Function: rand ()

Run select rand () in the query analyzer. The result is a random decimal number similar to this: 0.36361513486289558,
Decimal places like this are rarely used in practical applications. Generally, random numbers are used as random integers. Let's look at the following two random integer acquisition methods:
1,
A:
Select floor (RAND () * n) --- The generated number is as follows: 12.0
B:
Select cast (floor (RAND () * n) as INT) --- The generated number is as follows: 12

2.
A: Select ceiling (RAND () * n) --- The generated number is 12.0.
B: select cast (ceiling (RAND () * n) as INT) --- The generated number is as follows: 12
where N is a specified integer, such as 100, we can see that the method of the two methods has. the decimal value of 0, and the B method is the real integer.
generally, there is no difference between the two methods? Actually there is one thing, that is, the range of their random number generation:
the number range of Method 1: 0 to the N-1, such as cast (floor (RAND () * 100) as INT), any integer between 0 and 99 is generated.
the number range of method 2 is between 1 and N, for example, cast (ceiling (RAND () * 100) as INT) an integer between 1 and 100 is generated.
for this difference, you can see the help of SQL online:
comparison
compare ceiling and floor
the ceiling function returns a minimum integer greater than or equal to the given numeric expression. The floor function returns the largest integer less than or equal to the given numeric expression. For example, for the numeric expression 12.9273, ceiling returns 13 and floor returns 12. The data types returned by floor and ceiling are the same as those of the input numeric expression.
random
now, you can use these two methods as needed to obtain the random number ^_^
In addition, you need to remind cainiao, the method for randomly obtaining any N records in a table is very simple. Use newid ():
select Top N * From table_name order by newid () ---- N is an integer you specify, and the table is the number of records obtained
OK, this Article is written here

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.