[SQL] - Random Dates / Data for testing

NewID() is a really useful SQL function, since it's resolved on a per row rather than per query basis. That comes in handy if you want random values for each row in a table.

For example, this (ad-hoc) query will update your [datefield] with a random date within the past five days:

update [tablename] 
set [datefield] =
cast(
cast(getdate() as int)
-5*rand(cast(cast(newid() as binary(8)) as int))
as datetime)
There's another (more well known) trick for getting results back in random order that I might as well mention while I'm at it:
select * from [tablename] order by newid()

2 Comments

Comments have been disabled for this content.