## Generating Test Data: Part 2 - Generating Sequential and Random Dates

 Author Message Jeff Moden SSC Guru Group: General Forum Members Points: 510164 Visits: 44290 dwain.c (4/25/2012)You da man Jeff! Another great article!I've seen quite a few forum posts recently where your discussion of how DATETIMEs actually work would have assisted the posters had they understood this fundamental concept.There's quite a bit more as to how they actually work behind the scenes but few people ever need to go there... including me. ;-)Thanks for the feedback, Dwain. --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Jeff Moden SSC Guru Group: General Forum Members Points: 510164 Visits: 44290 GPO (4/25/2012)Superbly balanced article. But in true Oliver Twist fashion, "Can I have some more?"In the real-world datetime data I've dealt with, they often follow roughly cyclical patterns, not pure random patterns. For example, if you look at say ambient temperature in the town in which you live, over time it might approximate a sort-of sine wave from day to day, but also the larger sine wave of seasonal change. Similarly, if you look at something like Emergency Room attendances, there is a daily, weekly, and seasonal repeating pattern of attendance numbers. If you want to generate test ER attendance datetimes, you don't want them to be strictly random, because in the long run, you'll generate about the same number of ER attendances (for example) for 3:00AM as you will for 3:00PM. You instead, want it to be random within certain parameters. If you knew the mean and standard deviation of the number of attendances of the 24 hours of the day, by the seven days of the week, by the 4 (in my case) seasons of the year (672 rows of reference data), I'm wondering whether you could combine that knowledge with Jeff's techniques to generate test data that closely approximates the patterns seen in reality. "More please?" is the normal question after any such introductory article and I thank you for your thoughtful feedback.I've done such things in more of a stepped fashion but not in a nice, smooth sinusoidal fashion. I suppose that it just a matter of making smaller steps. I'll have to think about that. Thank you, again, for the feedback. --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Cadavre SSC-Insane Group: General Forum Members Points: 22098 Visits: 8519 Nicely explained Jeff.I may have to "borrow" your method of generating random DATETIME data, my method is more difficult to understand when people glance at it. :-D Forever trying to learnFor better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/If you litter your database queries with nolock query hints, are you aware of the side effects?Try reading a few of these links...(*) Missing rows with nolock(*) Allocation order scans with nolock(*) Consistency issues with nolock(*) Transient Corruption Errors in SQL Server error log caused by nolock(*) Dirty reads, read errors, reading rows twice and missing rows with nolockCraig Wilkinson - Software EngineerLinkedIn Jeff Moden SSC Guru Group: General Forum Members Points: 510164 Visits: 44290 TheSQLGuru (4/25/2012)Hey Jeff, can you put a downloadable file with the relevant operational code parts of the post? Thanks in advance, and wonderful stuff as always!Heh... stealing an idea from Lotus isn't my idea of a laudable defense. ;-) --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Jeff Moden SSC Guru Group: General Forum Members Points: 510164 Visits: 44290 Cadavre (4/26/2012)Nicely explained Jeff.I may have to "borrow" your method of generating random DATETIME data, my method is more difficult to understand when people glance at it. :-DBorrow away. These aren't my methods. They pretty well standard for folks that have been using NEWID() for such things over the years because they fall into the classic random number mathematical formulas.If you don't mind, could you post your method? It's always interesting to see how others do things. --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Jeff Moden SSC Guru Group: General Forum Members Points: 510164 Visits: 44290 GPO (4/25/2012)The spam people clearly target the articles that everyone is going to read...It's ironic that I have the SPAM people to thank for such a nice compliment. Thanks, GPO. --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Cadavre SSC-Insane Group: General Forum Members Points: 22098 Visits: 8519 Jeff Moden (4/26/2012)Cadavre (4/26/2012)Nicely explained Jeff.I may have to "borrow" your method of generating random DATETIME data, my method is more difficult to understand when people glance at it. :-DBorrow away. These aren't my methods. They pretty well standard for folks that have been using NEWID() for such things over the years because they fall into the classic random number mathematical formulas.If you don't mind, could you post your method? It's always interesting to see how others do things.Certainly. Generally, when I need "random" datetime data I go with this: -`IF object_id('tempdb..#testEnvironment') IS NOT NULLBEGINDROP TABLE #testEnvironmentEND--1,000,000 "Random" rows of dataSELECT TOP 1000000 IDENTITY(INT,1,1) AS ID, RAND(CHECKSUM(NEWID())) * 366 /*(Number of days)*/ + CAST('2000' AS DATETIME) /*(Start date, e.g. '2000-01-01 00:00:00'*/ AS randomDateINTO #testEnvironmentFROM master.dbo.syscolumns sc1, master.dbo.syscolumns sc2, master.dbo.syscolumns sc3;`If instead I want "random" date, I normally go with this: -`IF object_id('tempdb..#testEnvironment') IS NOT NULLBEGINDROP TABLE #testEnvironmentEND--1,000,000 "Random" rows of dataSELECT TOP 1000000 IDENTITY(INT,1,1) AS ID, DATEADD(DAY,((ABS(CHECKSUM(NEWID())) % 366 /*(Number of days)*/) + 1),CAST('2000' AS DATE) /*(Start date, e.g. '2000-01-01*/) AS randomDateINTO #testEnvironmentFROM master.dbo.syscolumns sc1, master.dbo.syscolumns sc2, master.dbo.syscolumns sc3;`On an internal wiki-page at work, I long since added a page with a brief explanation of how to create pseudo-random data. The script looks like this: -`--Standard TestEnvironment of 1,000,000 rows of random-ish dataIF object_id('tempdb..#testEnvironment') IS NOT NULLBEGINDROP TABLE #testEnvironment;END;--1,000,000 Random rows of dataSELECT TOP 1000000 IDENTITY(INT,1,1) AS ID, RAND(CHECKSUM(NEWID())) * 30000 + CAST('1945' AS DATETIME) AS randomDateTime,--SQL SERVER 2008 ONLY!! ONLY FOR USE ON 9.0 AND ABOVEDATEADD(DAY,((ABS(CHECKSUM(NEWID())) % 366) + 1),CAST('2000' AS DATE)) AS randomDate,ABS(CHECKSUM(NEWID())) AS randomBigInt,(ABS(CHECKSUM(NEWID())) % 100) + 1 AS randomSmallInt,RAND(CHECKSUM(NEWID())) * 100 AS randomSmallDec,RAND(CHECKSUM(NEWID())) AS randomTinyDec,RAND(CHECKSUM(NEWID())) * 100000 AS randomBigDec,CONVERT(VARCHAR(6),CONVERT(MONEY,RAND(CHECKSUM(NEWID())) * 100),0) AS randomMoneyINTO #testEnvironmentFROM master.dbo.syscolumns sc1, master.dbo.syscolumns sc2, master.dbo.syscolumns sc3;` Forever trying to learnFor better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/If you litter your database queries with nolock query hints, are you aware of the side effects?Try reading a few of these links...(*) Missing rows with nolock(*) Allocation order scans with nolock(*) Consistency issues with nolock(*) Transient Corruption Errors in SQL Server error log caused by nolock(*) Dirty reads, read errors, reading rows twice and missing rows with nolockCraig Wilkinson - Software EngineerLinkedIn Jeff Moden SSC Guru Group: General Forum Members Points: 510164 Visits: 44290 I see what you mean. Same principle... just harder for some folks to see. Thanks, Craig. --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Jeff Moden SSC Guru Group: General Forum Members Points: 510164 Visits: 44290 TheSQLGuru (4/25/2012)Hey Jeff, can you put a downloadable file with the relevant operational code parts of the post? Thanks in advance, and wonderful stuff as always!If I understand correctly, those are pretty well summarized in the last two sections of the article. Is that what you want as a downloadable file? --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Samrat Bhatnagar Forum Newbie Group: General Forum Members Points: 7 Visits: 353 These two part series were really useful. Thanks.Any suggestions on how to generate test data for following scenarios:1. Two tables linked using PK-FK relationship e.g. Product Category and Product Subcategory2. Self Referential Tables like the Employee table with EmployeeId, ManagerId, 3. Using the master tables in 1, 2 generate a table that has ProductFK, EmployeeFK, as in a Data warehouse.