SQL newid () random function

Source: Internet
Author: User
Tags floor function

COPIED from:http://www.cnblogs.com/wuming/archive/2010/02/24/1672906.html

From a table random 2 records, with select TOP * from Ywle ORDER by NEWID () the order by is generally sorted by a field, the return value of newid () is uniqueidentifier, order by Newi D () How to randomly select records newid () generates a value when each record is scanned, and the resulting values are random and have no case order.   So the final result is sorted again, the result of the sorting is of course unordered or select top *,newid () as Random from Ywle where Ywlename= ' 001 ' Ordey By Random the efficiency is higher because NEWID () returns a unique value of type uniqueidentifier. NEWID () each time the value is different, then the order is sorted according to such value, each time the result is not the same.

The principle is to take all the IDs out and then use the random function to get one, and then use the random ID to go to the database and then take out the records, all the cost is a bit large.

Random functions of SQL Server NEWID () and Rand ()

SELECT * from Northwind. Orders ORDER by NEWID ()--random sort

SELECT TOP Ten * from Northwind. Orders ORDER by NEWID ()--10 records randomly removed from the Orders table

Example

A. Using the NEWID function with variables The following example uses NEWID () to assign a value to a variable declared as a uniqueidentifier data type. The value is output before testing the value of the uniqueidentifier data type variable. --Creating a local variable with declareset syntax. DECLARE @myid uniqueidentifier Set @myid = NEWID () PRINT ' Value of @myid is ' + CONVERT (varchar (255), @myid) Here is the result set: Value Of @myid is 6F9619FF-8B86-D011-B42D-00C04FC964FF Note: NEWID returns different values for each computer. The figures shown are only useful for interpreting.

Random function: rand () executes in Query Analyzer: Select rand (), you can see that the result is similar to a random decimal: 0.36361513486289558, a decimal like this is used in practical applications, usually take random numbers will take random integers. Let's look at the following two methods of randomly taking integers:

1. A:select Floor (rand () *n)---Generated number is this: 12.0 B:select cast (Floor (rand () *n) as int)---number generated is this: 12

2. A:select Ceiling (rand () * N)---The number generated is this: 12.0 B:select cast (Ceiling (rand () * n) as int)---number generated is this: 12

where n is an integer you specify, such as 100, you can see that the a method of the two methods is a decimal with the. 0, and the B method is the real integer. In general, there is no difference between the two methods, really no difference? There is a point, that is their range of generated random numbers: The numeric range of Method 1: between 0 and N-1, such as CAST (Floor (rand () *100) as int) generates a range of numbers from 0 to 99 for any integer Method 2: Between 1 and N, such as cast ( Ceiling (rand () *) as int) will generate any integer between 1 and 100 for this difference, see SQL online Help on the cicada:-------------------------------------------------- ----------------------------------

Compare CEILING and floor

The CEILING function returns the smallest integer greater than or equal to the given numeric expression. The floor function returns the largest integer less than or equal to the given number expression. For example, for a numeric expression 12.9273,ceiling will return 13,floor will return 12. Floor and CEILING The data type of the return value is the same as the data type of the input numeric expression. ----------------------------------------------------------------------------------now, you can use these two methods to get random numbers based on your own needs. ^_^

In addition, you have to hint to the rookie, about random access to any N records in the table method, very simple, with newid (): Select Top N * FROM table_name ORDER BY NEWID ()----N is an integer you specify, the table is the number of records obtained.

From a table random 2 records, with select TOP * from Ywle ORDER by NEWID () the order by is generally sorted by a field, the return value of newid () is uniqueidentifier, order by Newi D () How to randomly select records newid () generates a value when each record is scanned, and the resulting values are random and have no case order.   So the final result is sorted again, the result of the sorting is of course unordered or select top *,newid () as Random from Ywle where Ywlename= ' 001 ' Ordey By Random the efficiency is higher because NEWID () returns a unique value of type uniqueidentifier. NEWID () each time the value is different, then the order is sorted according to such value, each time the result is not the same.

The principle is to take all the IDs out and then use the random function to get one, and then use the random ID to go to the database and then take out the records, all the cost is a bit large.

Random functions of SQL Server NEWID () and Rand ()

SELECT * from Northwind. Orders ORDER by NEWID ()--random sort

SELECT TOP Ten * from Northwind. Orders ORDER by NEWID ()--10 records randomly removed from the Orders table

Example

A. Using the NEWID function with variables The following example uses NEWID () to assign a value to a variable declared as a uniqueidentifier data type. The value is output before testing the value of the uniqueidentifier data type variable. --Creating a local variable with declareset syntax. DECLARE @myid uniqueidentifier Set @myid = NEWID () PRINT ' Value of @myid is ' + CONVERT (varchar (255), @myid) Here is the result set: Value Of @myid is 6F9619FF-8B86-D011-B42D-00C04FC964FF Note: NEWID returns different values for each computer. The figures shown are only useful for interpreting.

Random function: rand () executes in Query Analyzer: Select rand (), you can see that the result is similar to a random decimal: 0.36361513486289558, a decimal like this is used in practical applications, usually take random numbers will take random integers. Let's look at the following two methods of randomly taking integers:

1. A:select Floor (rand () *n)---Generated number is this: 12.0 B:select cast (Floor (rand () *n) as int)---number generated is this: 12

2. A:select Ceiling (rand () * N)---The number generated is this: 12.0 B:select cast (Ceiling (rand () * n) as int)---number generated is this: 12

where n is an integer you specify, such as 100, you can see that the a method of the two methods is a decimal with the. 0, and the B method is the real integer. In general, there is no difference between the two methods, really no difference? There is a point, that is their range of generated random numbers: The numeric range of Method 1: between 0 and N-1, such as CAST (Floor (rand () *100) as int) generates a range of numbers from 0 to 99 for any integer Method 2: Between 1 and N, such as cast ( Ceiling (rand () *) as int) will generate any integer between 1 and 100 for this difference, see SQL online Help on the cicada:-------------------------------------------------- ----------------------------------

Compare CEILING and floor

The CEILING function returns the smallest integer greater than or equal to the given numeric expression. The floor function returns the largest integer less than or equal to the given number expression. For example, for a numeric expression 12.9273,ceiling will return 13,floor will return 12. Floor and CEILING The data type of the return value is the same as the data type of the input numeric expression. ----------------------------------------------------------------------------------now, you can use these two methods to get random numbers based on your own needs. ^_^

In addition, you have to hint to the rookie, about random access to any N records in the table method, very simple, with newid (): Select Top N * FROM table_name ORDER BY NEWID ()----N is an integer you specify, the table is the number of records obtained.

SQL newid () random function

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.