Perhaps I'm misunderstanding how I should use the variant of the function you suggest.
Here is an example of how I would expect my version to be used, in this case for multiples of minutes.
Taking the 5th line as an example, if I wanted to have times truncated to 5 minute intervals I would use n = 5.declare @dt as datetime; set @dt = '2017-12-20 13:17:44'
select '1 min slot', dbo.datetrunc('mi', @dt, 1)
select '2 min slot', dbo.datetrunc('mi', @dt, 2)
select '3 min slot', dbo.datetrunc('mi', @dt, 3)
select '4 min slot', dbo.datetrunc('mi', @dt, 4)
select '5 min slot', dbo.datetrunc('mi', @dt, 5)
select '6 min slot', dbo.datetrunc('mi', @dt, 6)
select '10 min slot', dbo.datetrunc('mi', @dt, 10)
select '15 min slot', dbo.datetrunc('mi', @dt, 15)
select '20 min slot', dbo.datetrunc('mi', @dt, 20)
Which gives me (as the minutes portion)
1 -> 17, 2 -> 16, 3 -> 15, 4 -> 16, 5 -> 15, 6 -> 12, 10 ->10, 15 -> 15, 20 -> 0 - that is the portion truncated to multiples of n minutes.
I can't see how I can get your flavour to do that.
Here's another example. I want to see how many jobs have been created in each 5 minute interval for each day in some particular weekselect dbo.datetrunc('d', CreatedOn, 5), dbo.datetrunc('mi', CreatedOn, 5), count(*)
from tblJob
where CreatedOn >='2017-02-01' and CreatedOn < '2017-02-08'
group by dbo.datetrunc('d', CreatedOn, 5), dbo.datetrunc('mi', CreatedOn, 5)
order by 1,2
This lists the day, the time truncated to 5 minutes, and the count.
In my mind there is no meaning to a 7 or 23 minute truncation - if I wanted that then I'd use datepart and do a relevant division on that within the context of the date truncated to an hour or day.