SQL Server random number function _mssql

Source: Internet
Author: User
Tags floor function rand

But still hope that the veteran veterans more advice, now cut to the chase:
Random function: rand ()

Executing in Query Analyzer: Select rand (), and you can see that the result would be a random decimal like this: 0.36361513486289558,
Decimals like this are not used much in practical applications, and random integers are usually taken from random numbers. Look at the following two methods of randomly taking integers:
1,
A:
The number generated by the Select Floor (rand () *n)---is this: 12.0
B:
Select CAST (Floor (rand () *n) as int)---the number generated is this: 12

2,
The number of A:select ceiling (rand () * N)---Generated is this: 12.0
B:select cast (Ceiling (rand () * N) as int)---the number generated is this: 12
Where the n is an integer you specify, such as 100, you can see that the a method of the two methods is a decimal with a. 0, and the B method is a real integer.
Generally speaking, the two methods are no different, really no difference? There's a little bit of that, that's the range of their generated random numbers:
The number range of Method 1:0 through N-1, such as CAST (Floor (rand () *100) as int), generates any integer between 0 and 99
Method 2 has a range of digits between 1 and N, such as cast (ceiling rand () () as int), which generates any integer between 1 and 100
For this distinction, look at SQL's online Help for a cicada:
------------------------------------------------------------------------------------
Compare CEILING and FLOOR
The CEILING function returns the smallest integer that is 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 numeric expression 12.9273,ceiling will return 13,floor will return 12. The data types of the FLOOR and CEILING return values are the same as the data types of the numeric expressions entered.
----------------------------------------------------------------------------------
Now, you can use these two methods to get a random number of ^_^
In addition, but also to remind you rookie, about random access to any of the table N Records of the method, very simple, with newid ():
Select Top N * NEWID ()----n is an integer that you specify, and the table is the number of bars to get the record table_name
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.