Home > Database > Mysql Tutorial > 随机生成指定范围的日期

随机生成指定范围的日期

WBOY
Release: 2016-06-07 14:57:47
Original
1768 people have browsed it

在MSSQL中,有些商品会伪造一些评价,但是评价信息可能会集中在一天或几天内,现在需要把日期打散,平均分不到一个日期段内,那么这段代码就有用了。 无 DECLARE @EvId INT,@PiD INT;DECLARE Evaluate CURSOR FOR SELECT EvaluateId,ProductId FROM dbo.Produ

在MSSQL中,有些商品会伪造一些评价,但是评价信息可能会集中在一天或几天内,现在需要把日期打散,平均分不到一个日期段内,那么这段代码就有用了。
DECLARE @EvId INT,@PiD INT;
DECLARE Evaluate CURSOR FOR SELECT EvaluateId,ProductId FROM dbo.ProductEvaluate WHERE CreateTime > '2014-10-20 00:00:01';

OPEN Evaluate;
FETCH NEXT FROM Evaluate INTO @EvId,@PiD;
	WHILE @@FETCH_STATUS = 0
		BEGIN
			DECLARE @BDate datetime, @EDate DATETIME,@PDate DATETIME;
			SET @BDate = '20140101'          --下限
			SELECT @BDate = CONVERT(varchar(100), CreateTime, 112) FROM dbo.Products WHERE ProductId = @PiD;
			SET @EDate = '20141020 23:59:59' --上限
			UPDATE dbo.ProductEvaluate SET CreateTime =DATEADD(second,ABS(CHECKSUM(NEWID()))%DATEDIFF(second,@BDate,@Edate),@BDate) WHERE EvaluateId = @EvId ;
			FETCH NEXT FROM Evaluate INTO @EvId,@PiD;
		END
CLOSE Evaluate;
DEALLOCATE Evaluate;
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template