sql - beginning of hour, month etc

Viewed 74359

I know how to get in SQL (SQL Server) the current date, but with the beginning of the day:

select dateadd(DAY, datediff(day, 0, getdate()),0) 
(result:2009-09-17 00:00:00.000)

I need to get (in SQL) the current date with the beginning of this hour. For example: 2009-09-17 17:00:00 (I don't care about the exact format)

and I need to get the current date but with the beginning of this month: For example: 2009-09-01 00:00:00.000 (I don't care about the exact format)

Can you help me? Thanks in advance

6 Answers

From SQL Server 2022 you can use DATETRUNC for both these needs.

I need to get the current date with the beginning of this hour. For example: 2009-09-17 17:00:00

SELECT DATETRUNC(hour, GETDATE())

and I need to get the current date but with the beginning of this month: For example: 2009-09-01 00:00:00.000

SELECT DATETRUNC(month, GETDATE())
Related