I am trying to part a datetime value and build it together removing minutes, seconds and so on. I want the values to be hourly.

Usually this works on a SQL Server 2012 machine:

DATETIME2FROMPARTS( DATEPART(year, datum), DATEPART(month, datum), DATEPART(day, datum), DATEPART(hour, datum), 0, 0, 0, 0) as dtHour

But unfortunately in SQL Server 2008 it does not! Which function do I need, to fulfill the same job?

I also looked up this reference: Date&Time functions but this were all only SQL Server 2012 compliant.


You can make it a bit shorter.

dateadd(hour, datediff(hour, 0, datum), 0)

This calculates the number of hours between 1900-01-01 00:00:00.000 and datum and adds that value to 1900-01-01 00:00:00.000.

| improve this answer | |
  • nice : ) thank you! ps: i tried to upvote, but unfortunately SO doesnt allow me to upvote, because i dont have 15 rep. points – Erdinc Ay Nov 29 '13 at 12:18
  • 1
    You now have more than enough rep! – Max Vernon Nov 29 '13 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.