Random dates and times

  • 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.

     

     

  • 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

  • are you dealing with definite instances of time (point in time) in a range or period in time (time range)?


    Everything you can imagine is real.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply