考虑到开始和结束时间,我想知道在给定时间内有多少分钟被覆盖。
create function CalcMinsInHour(@start datetime, @end datetime, @hour int)
returns int
as
begin
--Looking for best way to write this part
end
CalcMinsInHour('2012-01-18 8:15', '2012-01-18 10:30', 7) should return 0
CalcMinsInHour('2012-01-18 8:15', '2012-01-18 10:30', 8) should return 45
CalcMinsInHour('2012-01-18 8:15', '2012-01-18 10:30', 9) should return 60
CalcMinsInHour('2012-01-18 8:15', '2012-01-18 10:30', 10) should return 30
CalcMinsInHour('2012-01-18 8:15', '2012-01-18 10:30', 11) should return 0
编辑:@Start和@End代表员工的时钟输入/输出时间。所以是的,当他们工作超过午夜时,他们可以跨越两天,但不会超过这个。
答案 0 :(得分:3)
这应该可以解决问题:
ALTER FUNCTION dbo.CalcMinsInHour(@start DATETIME, @end DATETIME, @hour INTEGER)
RETURNS INTEGER
AS
BEGIN
DECLARE @StartOfHour DATETIME
DECLARE @EndOfHour DATETIME
SELECT @StartOfHour = DATEADD(hh, @hour, CAST(CAST(@start AS DATE) AS DATETIME))
IF NOT (@StartOfHour BETWEEN @start and @end)
SET @StartOfHour = DATEADD(hh, @hour, CAST(CAST(@end AS DATE) AS DATETIME))
SELECT @EndOfHour = DATEADD(hh, 1, @StartOfHour)
RETURN
(
SELECT
CASE WHEN @EndOfHour < @start OR @StartOfHour > @end THEN 0 ELSE
DATEDIFF(mi,
CASE WHEN @StartOfHour <= @start THEN @start ELSE @StartOfHour END,
CASE WHEN @EndOfHour > @end THEN @end ELSE @EndOfHour END)
END
)
END
答案 1 :(得分:1)
你能使用DateDiff吗?
select DATEDIFF(Minute, '2011-11-10 00:00:59.900', '2011-11-10 00:01:00.100')
答案 2 :(得分:1)
这是计算总分钟数的函数(也适用于多天):
create function CalcMinsInHour(@start datetime, @end datetime, @hour int)
returns int
as
begin
declare @selecterHourIntervals table (HourStart datetime, HourEnd datetime)
declare @currentDate datetime
/* start in the @start date, but the hour we want to count */
set @currentDate = dateadd(hour, @hour, convert(datetime, convert(date, @start)))
/* for every day between @start-@end add out hour interval */
while @currentDate <= @end
begin
insert into @selecterHourIntervals values (
@currentDate,
dateadd(hour, 1, @currentDate))
set @currentDate = dateadd(day, 1, @currentDate)
end
declare @totalMinutes int
/* for every hour interval in the table, select number of minutes
trimmed by the @start - @end interval and sum */
select @totalMinutes = sum(DailyMinutes)
from
(select
datediff(minute,
case when HourStart > @start then HourStart else @start end,
case when HourEnd < @end then HourEnd else @end end) as DailyMinutes
from @selecterHourIntervals) TrimmedIntervals
where DailyMinutes > 0
return isnull(@totalMinutes, 0)
end