Updating dates with random time

时间:2015-05-12 23:30:01

标签: sql sql-server sql-server-2012

I have a $targets column, all of them at 12:00 am. Is there a way to update them with random hours, minutes to nearest 1/2 hour while keeping the same date(day) value?

datetime

1 个答案:

答案 0 :(得分:2)

Here's one option using async:

dateadd

And here's a good post about generating random numbers. Using that, multiple by 30 minutes to get to the nearest half hour.

Note, this uses update Activities set ActivityDate = DateAdd(minute, 30 * (abs(checksum(NewId())) % 47), ActivityDate); since there are 1440 minutes in a day -- that divides into 48 potential half hour segments in that same day.