要求在一表中插入这一时间段 > '2006-05-04 19:05'  < '2006-05-04 19:15' 范围内的随机时间
如:id         time
01      2006-05-04 19:06
02      2006-05-04 19:08
03      2006-05-04 19:11
04      2006-05-04 19:07
...        .... ... 
 ...      ....

解决方案 »

  1.   

    這個意思??可以借用臨時表Create Table TEST(ID Char(2),[time] Varchar(16))
    Create Table #T
    ([time] Varchar(16))
    Insert #T Select '2006-05-04 19:05'
    Union All Select '2006-05-04 19:06'
    Union All Select '2006-05-04 19:07'
    Union All Select '2006-05-04 19:08'
    Union All Select '2006-05-04 19:09'
    Union All Select '2006-05-04 19:10'
    Union All Select '2006-05-04 19:11'
    Union All Select '2006-05-04 19:12'
    Union All Select '2006-05-04 19:13'
    Union All Select '2006-05-04 19:14'
    Union All Select '2006-05-04 19:15'
    GO
    Insert TEST Select Top 1 '01',[time] From #T Order By NewID()
    Insert TEST Select Top 1 '02',[time] From #T Order By NewID()
    Insert TEST Select Top 1 '03',[time] From #T Order By NewID()
    GO
    Select * From TEST
    GO
    Drop Table TEST,#T
    --Result
    /*
    ID time
    01 2006-05-04 19:12
    02 2006-05-04 19:11
    03 2006-05-04 19:09
    */
      

  2.   

    可以借助几个函数组合起来
    select  dateadd(minute,rand(checksum(newid()))*10,'2006-05-04 19:05:00') --解释一下
    10为10分钟
    minute为时间单位
    后面的2006-05-04 19:05:00是时间起始时间
      

  3.   

    只要把上面的select作为子查询插入到表里就可以了
      

  4.   

    select dateadd(minute,rand()*10,getdate())
      

  5.   

    改寫下,這樣更簡單些。
    Create Table TEST(ID Char(2),[time] Varchar(16))
    GO
    Select TOP 100 ID=Identity(Int,1,1) Into # From SysColumns,SysObjects
    Select Convert(Varchar(16),DateAdd(mi,ID,'2006-05-04 19:04'),120) As [time] Into #T From # Where ID<=11
    Select * From #T
    Insert TEST Select Top 1 '01',[time] From #T Order By NewID()
    Insert TEST Select Top 1 '02',[time] From #T Order By NewID()
    Insert TEST Select Top 1 '03',[time] From #T Order By NewID()
    GO
    Select * From TEST
    GO
    Drop Table TEST,#T,#
    --Result
    /*
    ID time
    01 2006-05-04 19:12
    02 2006-05-04 19:11
    03 2006-05-04 19:09
    */
      

  6.   

    --由于datetime和Money类型可以相互转化,所以可以随机产生Money类型的数值,然后转化成datetime类型来达到目的.1.求两个边界时间的Money值.select cast(cast('2006-05-04 19:05' as datetime) as money)
    --38839.7951select cast(cast('2006-05-04 19:15' as datetime) as money)
    --38839.80212.然后产生38839.7951到38839.8021之间的随即数就可以达到目的了.--插入1000个此时间段的数值.create table #t([id] int,[time] datetime)
    Go
    declare @i int
    set @i=1while(@i<=1000)
    begin
    insert #t
    select @i,cast(38839+(7951+cast(rand()*70 as int))*1.0000/10000 as datetime)

    set @i=@i+1
    end
      

  7.   

    mschen(Co-ok) 提供了很好的思路 让人耳目一新
      

  8.   

    SELECT DATEADD(minute, RAND() * CAST(DATEDIFF(minute, '2006-05-04 19:05', 
          '2006-05-04 19:15') AS int), '2006-05-04 19:05') AS 时间