Wednesday, April 27, 2011

Generating random date data in SQL Server 2005

The statement below will generate random dates between 1 January 2010 and 27 Apr 2011.

select cast(cast('1 Jan 2010' as datetime) as int); --returns 40177
select cast(cast('27 Apr 2011' as datetime) as int) - cast(cast('1 Jan 2010' as datetime) as int); --returns 481

update transactions
set requesteddatetimeutc = cast((RAND(convert(varbinary,newid()))*481 + 40177) as datetime);

0 comments:

Post a Comment