SQL Server 2022 and the DATE_BUCKET function. Work more easily with dates
Hi guys,
Welcome back to all for another short but useful post!
This time I will tell you about another T-SQL TVF function introduced by the newest SQL Server 2022!
This function will permit us to easily cope with the needs that arise when we have to work with dates.
DATE_BUCKET
The DATE_BUCKET function collapses a date/time to a fixed interval and let me say that this is a great idea!
Now there is no more need to round datetime values, then extract date parts and perform conversions to and from other types like float.
Example
We can write:
declare @dt datetime2;
set @dt = '2022-03-21 19:45:01.123'
Select date_bucket(MONTH, 1, @dt)
Executing the command you will get:
All the dates will be rounded to a one month precision.
Prior to SQL Server 2022 we should have written:
declare @dt datetime2;
set @dt = '2022-03-21 19:45:01.123'
Select datefromparts( YEAR(@dt), MONTH(@dt), 1)
That's all for today!
p.s. ...are you genuinely interested to an Adverting showed on my blog? click it!
Previous post: SQL Server 2022 and the GREATEST / LEAST T-SQL commands
Comments
Post a Comment