午夜之前和之后的SQL Server计数 - 24小时

时间:2016-10-10 15:57:35

标签: sql-server datetime between

我需要在午夜之前和午夜之间使用24小时日期时间的字段计算记录。以下陈述适用于同一天的时间,例如:上午10:30到下午4:29之间:

WHERE CAST (theader_tdatetime AS time) BETWEEN '10:30' and '16:29' 

但是,我还需要从晚上7:31到凌晨2:00选择记录,我的陈述不再有效:

WHERE CAST (theader_tdatetime AS time) BETWEEN '19:31' and '02:00' 

如何从这段时间内选择记录?

编辑:一些桥接午夜差距的样本记录

2015-11-20 23:48:52.000  
2015-11-20 23:49:58.000  
2015-11-20 23:51:35.000  
2015-11-20 23:58:51.000  
2015-11-20 23:59:26.000  
2015-11-21 00:04:03.000  
2015-11-21 00:04:36.000  
2015-11-21 00:05:11.000  

2 个答案:

答案 0 :(得分:2)

WHERE CAST (transaction_date AS time)
BETWEEN '15:30 PM' and '23:59 PM' 
or CAST (transaction_date AS time) BETWEEN '0:00 AM' and '02:00 AM'

答案 1 :(得分:0)

WHAST CAST(theader_tdatetime AS time)> = '19:31'或CAST(theader_tdatetime AS time)< = '02:00'