Peter Larsson Blog

Patron Saint of Lost Yaks

A glance at SQL Server Denali CTP3 - DATEFROMPARTS

There is a new function in SQL Server Denali named DATEFROMPART. What is does, is to calculate a date from a number of user supplied parameters such as Year, Month and Date.

Previously you had to use a formula like this

DATEADD(MONTH, 12 * @Year + @Month - 22801, @Day)

to calculate the correct datevalue from the parameters. With the new DATEFROMPARTS, you simple write

DATEFROMPARTS(@Year, @Month, @Day)

and you get the same result, only slower by 22 percent. So why should you use the new function, if it's slower?
There are two good arguments for this

1) It is easier to remember
2) It has a built-in validator so that you cannot "spill" over the current month.

For the old way of doing this, using @Year = 2009, @Month = 2 and @Day = 29 you would end up with a date of 2009-02-28 and the DATEFROMPARTS will give you an error message.

Legacy Comments


research papers
2011-11-30
re: A glance at SQL Server Denali CTP3 - DATEFROMPARTS
This great blog is very interesting and enjoyable to read. I am a big fan of the subjects discussed. I also enjoy reading the comments, but notice that a lot of people should stay on topic to try and add value to the original blog post. I would also encourage everyone to bookmark this page to your favorite service to help spread the word.

employee schedule
2012-01-28
re: A glance at SQL Server Denali CTP3 - DATEFROMPARTS
This is helpful information to anybody that relies on SQL server technology. I can see how ensuring the date is correct can be a huge issue. Take for instance a software program that helps to create an employee schedule. If the date fields are not correct then the schedule that would be generated would be of no use. Thank you all for taking the time to spread your tips and helpful hints to the rest of the software community. Your hard work is appreciated.