存储过程中的T-SQL重叠时间范围参数
我想搜索发生在一天中特定时间和日期/时间范围之间的记录 例如: 我的桌子:存储过程中的T-SQL重叠时间范围参数,sql,tsql,stored-procedures,sql-server-2008-r2,Sql,Tsql,Stored Procedures,Sql Server 2008 R2,我想搜索发生在一天中特定时间和日期/时间范围之间的记录 例如: 我的桌子: ID | EmpID | AuthorizationTime ------------------------------- 1 | 21455 | '23/01/2012 12:44' 2 | 22311 | '23/01/2012 18:15' 3 | 21455 | '23/01/2012 23:04' 4 | 10222 | '24/01/2012 03:31' 5 | 21456 | '24/01/
ID | EmpID | AuthorizationTime
-------------------------------
1 | 21455 | '23/01/2012 12:44'
2 | 22311 | '23/01/2012 18:15'
3 | 21455 | '23/01/2012 23:04'
4 | 10222 | '24/01/2012 03:31'
5 | 21456 | '24/01/2012 09:00'
6 | 53271 | '25/01/2012 12:15'
7 | 10222 | '26/01/2012 18:30'
8 | 76221 | '27/01/2012 09:00'
SP输入参数示例:
@from: 22/01/2012 08:00
@to: 24/01/2012 23:00
@fromtime: 18:30
@totime: 08:00
预期产出:
EntryID EmployeeID AuthorisationTime
3 21455 '23/01/2012 23:04'
4 10222 '24/01/2012 03:31'
我在SP中尝试了以下select语句:
...
Select @wAuthorizationTime=' AuthorizationTime between ''' + CONVERT(nvarchar(30), @from )+ ''' and ''' + convert(nvarchar(50),@to )+ ''' '
Select @Where = @wAuthorizationTime; Declare @wHours nvarchar(1000)='';
if (ISNULL(@fromtime,'')<>'' and ISNULL(@ToTime,'')<> '') begin Select @wHours= ' (Cast(AuthorizationTime as time) between ''' + @fromTime + ''' and '''+ @ToTime +''')'
end if (@wHours <> '') Select @Where=@Where + ' and ' + @wHours
...
这句话的问题是,如果结束时间低于开始时间,例如23:00到03:00,我就得不到任何结果
如果我使用一个不重叠的时间范围,例如18:00到23:59,它确实有效
要获得以上结果,我需要做什么?添加一个检查,查看@fromtime>@totime是否正确。如果是这种情况,请按如下方式比较AuthorizationTime的时间强制值:
(cast(AuthorizationTime as time) between '00:00:00' and @totime) or
(cast(AuthorizationTime as time) between @fromtime and '23:59:59.999')
这将为您提供您想要的:
select *
from Times
where AuthorizationTime >= @from
and AuthorizationTime <= @to
and (
(@fromtime > @totime and ((cast(AuthorizationTime as time) between '00:00:00' and @totime) or
(cast(AuthorizationTime as time) between @fromtime and '23:59:59.999')
)
) or
(@fromtime <= @totime and cast(AuthorizationTime as time) between @fromtime and @totime)
)
-在SELECT语句之前执行复杂的操作
declare @from DateTime,
@to DateTime,
@fromTime int,
@totime int
-- sample data:
set @from = convert(datetime, '22/01/2012 08:00', 103)
set @to = convert(varchar, '24/01/2012 23:00', 103)
-- first truncte the date
set @from = convert(datetime, convert(varchar, @from, 103), 103)
set @to = convert(datetime, convert(varchar, @to, 103), 103)
-- then build the time as an int: Hour * 100 + Minute
set @fromTime = 1830
set @toTime = 800 -- note this is an int
-- Then use this to do the where clause:
select *
from myTable
where AuthorisationTime between @from and @to
and (DATEPART(HOUR, AuthorizationTime) * 100 + DATEPART(MINUTE, AuthorizationTime)
>= @fromTime OR
DATEPART(HOUR, AuthorizationTime) * 100 + DATEPART(MINUTE, AuthorizationTime)
<= @toTime)
我认为这很容易理解、测试,而且更有可能使用索引。这不起作用。问题的关键在于@fromTime有时比@toTime大。另外,我在测试数据上尝试了它,但它没有返回任何行。@Jerrad,你是对的,我有fromTime和toTime倒转。我在回答中改变了它。我也不确定缩短日期是否是你想要的。这不是我的问题,我只是另一个正在寻找答案的人。不过,它仍然没有返回任何行@是的。请看下面我的答案。@Jerrad我还有日期追溯到,从。这是固定的现在,我得到2行与您的样本数据。谢谢你指出这一点。