June 26, 2007 at 7:32 am
Given a date/time range how do I randomly select a date/time from from within that range? For example: 01012007:0300 to 060606:0300 and randomly select a date/time between those dates.
Thanks.
June 26, 2007 at 7:53 am
One way would be to calculate the difference between the two datetime values in seconds and mutliply this by an random number and add the result to the first datetime value:
declare @d1 datetime, @d2 datetime
set @d1 = '2007-01-1 03:00'
set @d2 = '2007-06-6 03:00'
select dateadd(second, datediff(second, @d1, @d2) * rand(), @d1 )
J
June 28, 2007 at 11:54 am
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply