--用视图辅助得到随机数 alterVIEW v_RAND AS SELECT re=RAND() GO --得到指定时间段的随机时间 alterfunction fn_getdate ( @begin_datedatetime, @end_datedatetime ) returnsvarchar(100) as begin declare@secondvarchar(50) if@begin_dateisnull SET@begin_date='2009-09-17 08:01:01'; if@end_dateisnull SET@end_date='2009-10-14 17:30:00'; SET@second=DATEDIFF ( second , @begin_date,@end_date) declare@d1datetime declare@randfloat select@rand=re from v_RAND set@d1=dateadd(second,@rand*@second,@begin_date) ifdatepart(hour,@d1) >18 begin set@d1=dateadd(hour,-8,@d1) end ifdatepart(hour,@d1) <8 begin set@d1=dateadd(hour,8,@d1) end return@d1 end go --测试 select dbo.fn_getdate(null,null)