SQL 将日期的秒钟随机语句怎么写

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL 将日期的秒钟随机语句怎么写相关的知识,希望对你有一定的参考价值。

例如:
表A 有如下日期字段
日期
2014-11-01 17:30:58
2014-11-02 19:30:58
2014-11-03 22:30:58
2014-11-04 17:30:58
2014-11-05 17:30:58
2014-11-06 17:30:58
2014-11-07 17:30:58
2014-11-07 17:30:58

将日期字段的秒钟 0-59秒内随机怎么写??

create table #(mark int)

go

declare @i int

set @i=0

while @i<=59

begin

  insert into # values(@i)

  set @i=@i+1

end

go

--随机数NewID办法

declare @myRand int

select top 1 @myRand=mark from # order by newid()

select @myRand

go

--Rand()办法(不过只是1-59)

declare @myRand int

select @myRand=cast(CEILING(rand()*59) as int)

select @myRand


--还是建议第一种


参考技术A dateadd(ss,(rand(checksum(newid()))*60-datepart(ss,[时间])),[时间])

以上是关于SQL 将日期的秒钟随机语句怎么写的主要内容,如果未能解决你的问题,请参考以下文章