Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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.

share|improve this question
    
The link you reference can display other versions of SQL Server, for instance this is for 2008: technet.microsoft.com/en-us/library/ms186724(v=sql.100).aspx –  Max Vernon Nov 29 '13 at 20:08
add comment

1 Answer

up vote 5 down vote accepted

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.

share|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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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