SQL修改一个时间字段秒数为随机数!

例如A表 有个字段 时间
2013-08-30 17.59.35
2013-09-01 17.59.35
2013-09-02 17.59.35
2013-09-03 18.59.35

修改9月份 秒钟数随机 其他不变

核心思路:用rand(checksum(newid()))生成0到1之间随机数,乘以60并减去原来的秒数,即可得到0到60之间的随机秒数。

create table a ([时间] datetime);
insert into a
select '2013-08-30 17:59:35' union all
select '2013-09-01 17:59:35' union all
select '2013-09-02 17:59:35' union all
select '2013-09-03 18:59:35'
 
update a set [时间]=dateadd(ss,(rand(checksum(newid()))*60-datepart(ss,[时间])),[时间])

结果如下

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-22
floor(dbms_random.value(0,60))产生0到59的随机数
第2个回答  2013-09-22
sql

update A set A.Date=left(A.Date,17)+Rand() where **
rand() 转换