DATE_BUCKET – SQL Server 2022

SQL Server 2022 brings some exciting new language features such as DATE_BUCKET

SELECT DATE_BUCKET(HOUR, 1, GETUTCDATE()) AS Bucket_Now

https://learn.microsoft.com/en-us/sql/t-sql/functions/date-bucket-transact-sql?view=sql-server-ver16

As per the information, this function returns the date-time value corresponding to the start of each date-time bucket. This means it much easier and more readable now to bucket into hours, days, weeks.

In our example above the UTC time is 14:15 and we’ve chosen hour buckets

The result from above is:

Bucket_Now
2022-10-31 14:00:00.000

Thanks for reading!

DATE_BUCKET – SQL Server 2022

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest