Sqlsever: How do I generate different random numbers for each row in select?

Source: Internet
Author: User

The random function of select is a bit false, perhaps because it is time-based, the same select because the time can not stagger the resulting random number is the same, how to make different rows have different random numbers?

The following is an example of a random date that produces a month.

--Create a view with a minimum of 1 maximum of 31 if object_id (' view_rand_int31 ') is not nullbegindrop view View_rand_int31endgocreate view View_rand_ Int31as Select CAST (Ceiling (rand ()) as int) as [r]go--creation Date (days) of the random function if object_id (' dbo. Fun_getrandday ') is not nullbegindrop function dbo. Fun_getranddayendgocreate FUNCTION dbo. Fun_getrandday (@max INT) returns intas begindeclare @r intselect @r = [R] from View_rand_int31while @r> @maxbeginselect @ R = [r] from view_rand_int31if @r<[email Protected]beginbreak;endendreturn @ rendgo--Test Select conditions to achieve multiple records simultaneously take random number--insert test data row declare @t table (rowNum int identity, [yearmonth] nvarchar ()) Declare @i int, @imax intSELECT @i=1, @imax =28while @i<[email Protected]begininsert into @t ([yearmonth]) Select ' February 2014 ' set @[email p rotected]+1end--executes the query select *, cast (' 2014-02-' + cast (dbo. Fun_getrandday (+) as varchar (2) as DateTime) as [Date], (select cast (Ceiling (rand () *) as int)) as [R] from @t



Sqlsever: How do I generate different random numbers for each row in select?

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.