我正在使用虚拟信息填充我的数据库用于测试目的,感谢http://mitchelsellers.com/blogs/2008/09/12/creating-random-sql-server-test-data.aspx
并且它正在工作,但是我的日期是随机的并且随机的年/月但是总是当前时间,一些示例从我的数据库:
2017-05-30 16:41:53.190
2017-07-21 16:41:53.193
2016-04-23 16:41:53.193
但是我使用这些日期的申请必须以
的形式提供2017-05-30 16:00:00.000
2017-07-21 01:30:00.000
2016-04-23 04:45:00.000
换句话说,日期目前正常,但minute
应该在00,15,30,45
上,而seconds
和miliseconds
应始终为00
。< / p>
是否可以将上述格式的随机时间添加到我的sql中?
我当前的SQL逻辑如下:
DECLARE @RowCount INT
DECLARE @RowString VARCHAR(10)
DECLARE @Random INT
DECLARE @Upper INT
DECLARE @Lower INT
DECLARE @InsertDate DATETIME
DECLAre @LoopThisManyTimes INT
SET @Lower = -360 /* move 360 days backwards */
SET @Upper = 360 /* move 360 days forward */
SET @RowCount = 0 /* start the counter at 0 */
SET @LoopThisManyTimes = 10 /* set the number of posts to add */
USE [Scheduler]
TRUNCATE TABLE [dbo].[Appointments] /* clear table for testing purposes */
WHILE @RowCount < @LoopThisManyTimes /*loop and add to table */
BEGIN
SET @RowString = CAST(@RowCount AS VARCHAR(10))
SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SET @InsertDate = DATEADD(dd, @Random, GETDATE())
答案 0 :(得分:1)
只需几秒钟而不是几天进行计算。这是一种方式:
WHILE @RowCount < @LoopThisManyTimes /*loop and add to table */
BEGIN
SET @RowString = CAST(@RowCount AS VARCHAR(10));
SELECT @Random = ROUND(((@Upper - @Lower -1)*60*60*24 * RAND() + @Lower*60*60*24), 0);
SET @InsertDate = DATEADD(second, @Random, GETDATE());
END;
编辑:
如果你想要固定的分钟数:
WHILE @RowCount < @LoopThisManyTimes /*loop and add to table */
BEGIN
SET @RowString = CAST(@RowCount AS VARCHAR(10));
SELECT @Random = ROUND(((@Upper - @Lower -1)*4*24 * RAND() + @Lower*4*24), 0);
SET @InsertDate = DATEADD(minute, @Random*15, CAST(GETDATE() as DATE));
END;
我认为算术是正确的。