Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert 1000 random dates between a given range?

I am new to sql server. I need to generate random dates selected from a given date range. Like the date of employment of an employee should be anywhere between 2011-01-01 and 2011-12-31. The generated dates should be inserted into a 1000 row table randomly.

Can any one guide me with my query?

like image 470
user1260815 Avatar asked Mar 10 '12 09:03

user1260815


People also ask

How do I create a random date range in Excel?

To generate random dates between two dates, you can use the RANDBETWEEN function, together with the DATE function. This formula is then copied down from B5 to B11. The result is random dates between Jan 1, 2016 and Dec 31, 2016 (random dates in the year 2016).


1 Answers

declare @FromDate date = '2011-01-01' declare @ToDate date = '2011-12-31'  select dateadd(day,                 rand(checksum(newid()))*(1+datediff(day, @FromDate, @ToDate)),                 @FromDate) 
like image 148
Mikael Eriksson Avatar answered Oct 14 '22 14:10

Mikael Eriksson