2

I've this:

SELECT GETDATE()

Which gives me :

2017-06-12 16:51:50.410

How can I convert this in order to get:

2017-06-12 16:00:00.000

I've some processes and I'm registering the date and hour of the job but I only want the exactly hour.

How can I do this?

Thanks

Pedro Alves
  • 1,004
  • 1
  • 21
  • 47

2 Answers2

4

To get getdate() truncated to hour:

select dateadd(hour, datediff(hour, 0, getdate() ), 0)

This adds the number of hours since 1900-01-01 to the date 1900-01-01.

You can also swap hour for other levels of truncation: year, quarter, month, day, minute, second, et cetera.

SqlZim
  • 37,248
  • 6
  • 41
  • 59
-1

I think the most human-readable solution is:

SELECT convert(datetime,FORMAT(GETDATE(),'yyyy-MM-dd HH:00:00.000'))
  • FORMAT to get the GETDATE timestamp exactly how you want it.

  • CONVERT to ensure the data type is a datetime.

GlennFriesen
  • 302
  • 4
  • 15