获取特定期间的订单数据

时间:2017-04-15 17:53:49

标签: sql sql-server sql-server-2012

我正在尝试在今天的日期之间获取订单数据,直到下午4:00凌晨。

实施例

SELF
等等......

我怎样才能做到这一点?

查询

Start date : 14/4/2017 12:00 AM
End date : 15/4/2017 4:00 AM

Start date : 16/4/2017 12:00 AM
End date : 17/4/2017 4:00 AM

2 个答案:

答案 0 :(得分:0)

在SQL-Server 2008及更高版本上:

declare @start_date datetime = cast(getdate() as date);
declare @end_date datetime = dateadd(hour, 4, dateadd(day, 1, @start_date)) 

select *
from   orders
where dt between @start_date and @end_date

基本上将一天和4小时添加到start_date。

declare @start_date datetime = cast(getdate() as date);
declare @end_date datetime = dateadd(hour, 4, dateadd(day, 1, @start_date))

select @start_date, @end_date;
GO
(No column name)    | (No column name)   
:------------------ | :------------------
15/04/2017 00:00:00 | 16/04/2017 04:00:00

dbfiddle here

答案 1 :(得分:0)

获取两个日期和特定时间之间的订单:

SELECT * FROM Orders WHERE date BETWEEN '14-04-17 00:00:00.00' AND '15-04-17 03:59:59.99'