如何使用随机日期更新行

Mar*_*tin 62 sql sql-server

我有一个简单的SQL表,它有一个DateTime列.我想用随机日期更新所有行(> 100000行).有一个简单的方法来执行这个SQL查询吗?

gbn*_*gbn 76

使用此命令生成1900年1月1日到2079年6月6日之间的smalldatetime(未选中,未安装SQL)

DATEADD(day, (ABS(CHECKSUM(NEWID())) % 65530), 0)
Run Code Online (Sandbox Code Playgroud)

NEWID比尝试使用RAND更好:RAND不会在单个SELECT或UPDATE中生成不同的值行(如果行为已更改,则不会在SQL 2000中生成).

编辑:像这样

UPDATE
  table
SET
  datetimecol = DATEADD(day, (ABS(CHECKSUM(NEWID())) % 65530), 0)
Run Code Online (Sandbox Code Playgroud)

编辑:将65535更改为65530并添加ABS以避免在范围上限溢出


Jho*_*re- 56

我将补充以下答案,

SELECT DATEADD(DAY, ABS(CHECKSUM(NEWID()) % 3650), '2000-01-01')
FROM your_table
Run Code Online (Sandbox Code Playgroud)

这会生成从2000-01-01开始的日期,并且您可以更改模数值中的天数,我放3650(约10年),这种方法不会溢出.

如果你想更新,那么

UPDATE your_table
SET your_date_field = DATEADD(DAY, ABS(CHECKSUM(NEWID()) % 3650), '2000-01-01')
WHERE your_conditions
Run Code Online (Sandbox Code Playgroud)

  • 是的,我的错误.CHECKSUM生成带符号的32位整数,所以我添加了ABS (2认同)

Pie*_*ems 32

这个问题似乎很老,但我的回答可能对其他人有用.

      Update table
      SET Time= DateAdd(d, ROUND(DateDiff(d, '2010-01-01', '2013-12-31') * RAND(CHECKSUM(NEWID())), 0),
      DATEADD(second,CHECKSUM(NEWID())%48000, '2010-01-01'))
Run Code Online (Sandbox Code Playgroud)

这会在给定范围之间生成随机日期时间.


Chr*_*all 8

我调整了上面的Jhonny的答案来获取过去10年的日期:

SELECT dateadd(day, (abs(CHECKSUM(newid())) % 3650) * -1, getdate())
Run Code Online (Sandbox Code Playgroud)

请注意,这只是SQLServer.


Pau*_*ham 6

我用它为我所有的测试数据设置了 1940 年到 1985 年之间的出生日期

SET [Birth Date] = DATEADD(day, (ABS(CHECKSUM(NEWID())) % 16250), '1940-1-1 00:00:00.001')
Run Code Online (Sandbox Code Playgroud)


CSh*_*per 5

以下代码将填充FiscalYear表的StartDate列,其中包含两个给定日期之间的随机日期:

-- First, let's declare the date range.
DECLARE @date_from DATETIME;
DECLARE @date_to DATETIME;

-- Set the start and date dates. In this case, we are using
-- the month of october, 2006.
SET @date_from = '1985-10-14';
SET @date_to = '2009-04-27';

UPDATE FiscalYear SET StartDate =  
(
    -- Remember, we want to add a random number to the
    -- start date. In SQL we can add days (as integers)
    -- to a date to increase the actually date/time
    -- object value.
    @date_from +
    (
        -- This will force our random number to be >= 0.
        ABS
        (
            -- This will give us a HUGE random number that
            -- might be negative or positive.
            CAST(CAST(NewID() AS BINARY(8)) AS INT)
        )

        -- Our random number might be HUGE. We can't have
        -- exceed the date range that we are given.
        -- Therefore, we have to take the modulus of the
        -- date range difference. This will give us between
        -- zero and one less than the date range.
        %

        -- To get the number of days in the date range, we
        -- can simply substrate the start date from the
        -- end date. At this point though, we have to cast
        -- to INT as SQL will not make any automatic
        -- conversions for us.
        CAST((@date_to - @date_from) AS INT)
    )
)
Run Code Online (Sandbox Code Playgroud)