|
--用视图辅助得到随机数
alter VIEW v_RAND
AS
SELECT re=RAND()
GO
--得到指定时间段的随机时间
alter function fn_getdate
(
@begin_date datetime,
@end_date datetime
)
returns varchar(100)
as
begin
declare @second varchar(50)
if @begin_date is null
SET @begin_date='2009-09-17 08:01:01';
if @end_date is null
SET @end_date='2009-10-14 17:30:00';
SET @second = DATEDIFF ( second , @begin_date,@end_date)
declare @d1 datetime
declare @rand float
select @rand=re from v_RAND
set @d1 = dateadd(second,@rand*@second,@begin_date)
if datepart(hour,@d1) >18
begin
set @d1=dateadd(hour,-8,@d1)
end
if datepart(hour,@d1) <8
begin
set @d1=dateadd(hour,8,@d1)
end
return @d1
end
go
--测试
select dbo.fn_getdate(null,null)
--结果
----------------------------------------------------------------------------------------------------
10 12 2009 9:23AM
(所影响的行数为 1 行)
|