如何从SQL中的当前时间获取所有时间在下一个小时范围内的数据

问题描述 投票:0回答:1

我有一张表tblSMSdatetimetime列(以及其他一些列)。

我在接下来的2小时内在仪表板上得到所有提醒,所以我想从今天datetime的表格中获取数据,time在接下来的2小时内。

我使用了这个存储过程,但它无法正常工作:

select * 
from tblSMS 
where  
    SMSTime >= (SELECT Convert(varchar(5), GetDate(), 108)) 
    and (SMSTime <= (SELECT Convert(varchar(5), (Select dateadd (hour, 2, getdate())), 108)))
    and Convert(varchar(10), SMSDate, 110) = Convert(varchar(10), GETDATE(), 110)

谢谢

sql-server sql-server-2008
1个回答
1
投票

如果您以适当的time格式存储,则无需使用datetime列。最好以datetime格式进行比较。否则,如果接下来的第二天下降2小时,您可能会遇到麻烦。

select * 
from tblSMS 
where  
    SMSDate between getdate() and dateadd(hh, 2, getdate())

如果要将SMSDate存储为日期,请合并两列以获取日期时间

select * 
from tblSMS 
where  
    SMSDate + cast(SMSTime as datetime) between getdate() and dateadd(hh, 2, getdate())
© www.soinside.com 2019 - 2024. All rights reserved.