Sql Server: Generate random DATE using RAND() function and GETDATE()
- Posted by Sqltimes
- On March 8, 2014
- 0 Comments
Another quick one today:
A few days ago, I wrote a post that illustrates how to generate dummy data; In that there are a couple of DATETIME columns that have random datetime data. This random values are based completely on the RAND() function in Sql Server. Subtracting a random number of days from GETDATE() function results in a date that is reasonable random DATETIME values, with in the last 3 years, that is useful for a good percentage of daily test scenarios — but not all.
Here it goes:
1
|
SELECT DATEADD( DAY , -1 * CEILING(RAND()*1000) , GETDATE()) |
Result:
1
2
3
4
|
----------------------- 2012-11-22 06:02:44.563 (1 row(s) affected) |
Hope this helps,
_Sqltimes
0 Comments