[SQL] - Random Dates / Data for testing - Jon Galloway

[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()
Published Thursday, March 18, 2004 10:24 PM by Jon Galloway
Filed under:

Comments

# re: [SQL] - Random Dates / Data for testing

What?! You're wasting precious Guids! Better hope the Guid police don't find out about this.


...joking of course... ;)

Friday, March 19, 2004 11:50 AM by Drew Marsh

# re: [SQL] - Random Dates / Data for testing

This was what I was surfing for.  thanks.

Thursday, September 4, 2008 10:45 AM by Rob