POST POST

JAN
31
2016

ProTip: Get a random date in SQL Server

Need to put a random date in each row of a table? Here is how to do it:

I have a table which contains a RequiredCompletionDate and I wanted to give it a random date in last year so I did

1
2
3
4
update systems 
set RequiredCompletionDate = DATEADD(day,
ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 365,
'2015-01-01')

Simon Timms

Email Email
Web Web
Twitter Twitter
GitHub GitHub
RSS

Looking for someone else?

You can find the rest of the Western Devs Crew here.

© 2015 Western Devs. All Rights Reserved. Design by Karen Chudobiak, Graphic Designer