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?
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).
declare @FromDate date = '2011-01-01' declare @ToDate date = '2011-12-31' select dateadd(day, rand(checksum(newid()))*(1+datediff(day, @FromDate, @ToDate)), @FromDate)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With