sql 添加时间段内随机时间

网上搜的例子 时间段内
DECLARE @dt1 DATETIME,@dt2 DATETIME,@a BIGINT,@b BIGINT
SET @dt1='2010-01-01'--開始日期
SET @dt2='2010-06-30'--結束日期
 
SELECT @a=CAST(@dt1 AS BIGINT),@b=CAST(@dt2-@dt1 AS BIGINT)
 
DECLARE @t TABLE(ID INT,dt datetime)
 
DECLARE @i INT
SET @i=0
 
WHILE @i<100
BEGIN
    INSERT @t
    SELECT 
        @i+1,CAST(ABS(CHECKSUM(newID()))%@b+@a AS DATETIME)+RAND() 
    SET @i=@i+1
end
SELECT * FROM @t

年月日,时分秒 都是随机的

修改评论中的时间

DECLARE @dt1 DATETIME,@dt2 DATETIME,@a BIGINT,@b BIGINT
SET @dt1='2010-01-01'--開始日期
SET @dt2='2010-06-30'--結束日期
 
SELECT @a=CAST(@dt1 AS BIGINT),@b=CAST(@dt2-@dt1 AS BIGINT)
update dbo.EvaluationReply SET  time= CAST(ABS(CHECKSUM(newID()))%@b+@a AS DATETIME)+RAND()

--修改时间中的时分秒为创建时间的时分秒
UPDATE Evaluation SET Time=convert(char(10),Time,120)+' '+convert(char(8),CreateTime,114) 

写个游标修改评论下的回复 时间,不能超过当前评论的时间

ALTER PROC [dbo].[Proc_UpdateEvaluationReplyCnt]
AS
BEGIN

DECLARE @id INT 
DECLARE @time DATETIME
DECLARE CURStaus1_PLAN CURSOR FOR
    --查询所有用户信息
        SELECT id,time FROM dbo.Evaluation 
        OPEN CURStaus1_PLAN
    FETCH NEXT FROM CURStaus1_PLAN INTO @id,@time
    WHILE (@@FETCH_STATUS = 0)
    BEGIN

DECLARE @dt1 DATETIME,@dt2 DATETIME,@a BIGINT,@b BIGINT
SET @dt1=CONVERT(varchar(100), @time, 23)--開始日期
SET @dt2=CONVERT(varchar(100), dateadd(day,7,@time), 23)--結束日期
 
SELECT @a=CAST(@dt1 AS BIGINT),@b=CAST(@dt2-@dt1 AS BIGINT)

UPDATE dbo.EvaluationReply SET  time=
CAST(ABS(CHECKSUM(newID()))%@b+@a AS DATETIME)+RAND()  where  EvaluationId=@id


    FETCH NEXT FROM CURStaus1_PLAN INTO @id,@time
    END 
    CLOSE CURStaus1_PLAN
    DEALLOCATE CURStaus1_PLAN
    
END

 

posted @ 2014-05-22 17:57  艾紫霁  阅读(1865)  评论(0编辑  收藏  举报