RAND not different for every row in T-SQL UPDATE

前端 未结 2 690
你的背包
你的背包 2020-12-06 04:21

I have the follow T-SQL to update a table with test data:

UPDATE
SomeTable
SET
    Created = GETDATE ( ) - CAST ( RAND ( ) * 365 AS int ) ,
    LastUpdated =         


        
相关标签:
2条回答
  • if you only want days from the past year use this (based on @gbn's answer):

    select GETDATE ( ) - ABS( CHECKSUM(NEWID()) % 365 )
    
    0 讨论(0)
  • 2020-12-06 05:27

    Use RAND(CHECKSUM(NEWID()))

    • NEWID returns a GUID
    • CHECKSUM makes it int, randomly
    • The int seeds the RAND

    In your case, you could modulo the checkum because CHECKSUM(NEWID()) is already random.

    CHECKSUM(NEWID()) % 365
    
    0 讨论(0)
提交回复
热议问题