0

如何使用 MSSQL 动态获取一天之间的范围,例如 BETWEEN 23.05.2012 00:00 AND 23.05.2012 23:59?
我得到了第一部分;

WHERE
AND s.SCHEDULE_START_DATE BETWEEN dateadd(DAY, datediff(DAY, 0, getdate()), 0) AND FILL HERE
4

2 回答 2

1

你将不得不尝试类似的东西

s.SCHEDULE_START_DATE >= dateadd(DAY, datediff(DAY, 0, getdate()), 0)
AND s.SCHEDULE_START_DATE < (dateadd(DAY, datediff(DAY, 0, getdate()), 0) + 1)

看看下面的例子

SQL 小提琴演示

于 2013-05-23T08:23:20.203 回答
1

试试这个——

询问:

DECLARE @Dates TABLE
(
    SCHEDULE_START_DATE DATETIME
)

INSERT INTO @Dates 
VALUES 
    ('20130522'),
    ('20130523'),
    ('20130524'),
    ('20130523 18:00:00'),
    ('20130523 23:59:59')

DECLARE 
      @DateFrom DATETIME = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
    , @DateTo DATETIME = DATEADD(SECOND, 86399, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0))

SELECT *
FROM @Dates
WHERE SCHEDULE_START_DATE BETWEEN @DateFrom AND @DateTo

输出:

SCHEDULE_START_DATE
-----------------------
2013-05-23 00:00:00.000
2013-05-23 18:00:00.000
2013-05-23 23:59:59.000
于 2013-05-23T08:25:35.200 回答