SQL Server 2012 introduces several new date/time functions that allow you to build a date/time from the individual parts of a date/time value. As I was experimenting with them, I was thinking that this is pretty neat, and bound to be very useful. And then I started wondering how well it performed.

Before I had time to test it, I was working on something else where I needed to truncate a date/time value to the first day of the month. I immediately whipped out my toolbelt – it has the routine that I learned from Lynn Pettis a few years back on SQLServerCentral.com from his blog post on Common Date Routines. This routine does the DateAdd(MONTH, DateDiff(MONTH, ‘1900-01-01’, @Date), ‘1900-01-01’) trick so see how many month boundaries exist from a known date to the tested date, and then to add this number of month boundaries back to the known date. With the month datepart being used, the result ends up being the first of the month of the tested date. Using other dateparts gives us other dates with remainders being truncated.

As I was looking at that wonderfully efficient code, I realized that I could get the same results with the DATETIMEFROMPARTS function in SQL Server 2012. So I coded an example, and it worked beautifully.

And now, I had a burning question. Performance-wise, just how do these different methods of truncating dates compare against each other? So, stop everything that I’m doing, and code a test.

First things first. A performance test requires a bunch of test data. And I mean a bunch. So, I grabbed some code from an article that Jeff Moden wrote on SQLServerCentral.com at the beginning of 2011. Sure enough, there’s a million row dataset of random dates… just what I’m looking for. So, by borrowing some code from Jeff, I have my test data:

Next up is to generate a test and to produce timings. In the following code, I truncate each date to the first day of the year, month and quarter, and then I truncate the time down to the hour, minute, and second. I perform this conversion twice, the first time using Lynn’s DateAdd/DateDiff code, and the second time by stripping out the various parts of the date, and putting it back together with the DATETIMEFROMPARTS function. Both queries dump the results into temp tables. I run this block of code with SET STATISTICS TIME ON just before each query to capture the time to run each query, and that batch is performed 25 times.

For the DateAdd/DateDiff, each calculation is counting the number of boundaries being crossed. Each boundary that we’re truncating the date to has to be counted individually. To use the SQL Server 2012 DATETIMEFROMPARTS function, we first have to extract each appropriate part before they can be passed back into the function to build the date; this code extracts it out once for each record in the CROSS APPLY, and then uses that value in each of the functions.

First up, the code:

I then took the results, and used a split function to get the timings from each query into a result set. This was coped into Excel, where the following line graphs tell the rest of the story:



I think I’ll continue to use Lynn’s DateAdd/DateDiff method when I need to truncate a date.

(Test environment: Windows 7 x64; SQL Server 2012 x64; 8GB ram)