我有下表:
oDate oTime oAct
--------------------------------------
2017-06-01 00:00:00 A
2017-06-01 01:00:00 B
2017-06-01 02:00:00 C
ff.
2017-06-02 00:00:00 B
ff.
我想在前一天(仅在21:00:00之后)和之后选择
假设,如果我选择“2017-06-02”,则结果应为:
oDate oTime oAct
--------------------------------------
2017-06-01 22:00:00 A
2017-06-02 00:00:00 B
2017-06-02 01:00:00 C
ff.
2017-06-03 00:00:00 C
ff.
另外,对于查询。我只有一个参数,即@oDate date
请指教。
谢谢。
答案 0 :(得分:2)
如果我正确地按照你的问题,我认为你正在遵循这样的where子句:
select
*
from YourTable
where (
oDate > '20170602'
OR
(oDate = '20170602' AND oTime >= '21:00:00')
)
这将为您提供2017-06-02之后的所有日期以及该日期21:00及之后的时间
为了充分利用这些列上可能存在的索引,我建议您不尝试将日期与时间结合起来,例如dateadd(day,datediff(day,0,oDate), oTime)然后尝试过滤> =' 20170602 21:00:00'因为这会产生表扫描。
也许这会有所帮助
select
*
from YourTable
where (
oDate > @dateparameterhere
OR
(oDate = @dateparemterhere AND oTime >= '21:00:00')
)
答案 1 :(得分:0)
试试这个 连接字段以创建日期时间字段
select
* from table
where
cast (cast(odate as varchar(max)) + ' ' + cast(otime as varchar(max)) as datetime) < cast('2017-06-02 21:00:00' as datetime)