我需要根据日期和时间选择数据。我有两个标准。如何实现这一点。
1)选择1-1-2013和1-10-2013之间的数据,时间范围在10到16之间
2)选择1-1-2013和1-10-2013之间的数据,时间范围在第二天早上20到8之间
我实现了一个代码。它只适用于第一个标准。这就是代码。
where date>='1-1-2013' AND date <'1-10-2013'
AND CAST(date AS TIME) between '10:00' and '16:00'
这里表中的日期字段是datetime type.Pleases帮助解决这个问题
答案 0 :(得分:0)
1)
WHERE Date Between '2013-01-01 10:00' AND '2013-10-01 16:00'
2)
WHERE Date Between '2013-01-01 20:00' AND '2013-10-01 08:00'
答案 1 :(得分:0)
试试这个:
DECLARE @tmp TABLE ( date DATETIME )
INSERT INTO @tmp
( date )
VALUES ( '2013-01-01 10:09:29' -- date - datetime
)
INSERT INTO @tmp
( date )
VALUES ( '2013-01-01 15:09:29' -- date - datetime
)
INSERT INTO @tmp
( date )
VALUES ( '2013-01-01 17:09:29' -- date - datetime
)
INSERT INTO @tmp
( date )
VALUES ( '2013-01-01 07:09:29' -- date - datetime
)
SELECT date
FROM @tmp AS t
WHERE CONVERT(DATE,date) >= CONVERT(DATE, '01-01-2013', 105)
AND CONVERT(DATE,date) <= CONVERT(DATE, '01-01-2013', 105)
AND CONVERT(TIME, date) BETWEEN CONVERT(TIME, '10:00')
AND CONVERT(TIME, '16:00')
答案 2 :(得分:0)
我理解正确吗?我不知道..
试试这个
where date>='1-1-2013' AND date <'1-10-2013'
AND ((CAST(date AS TIME) between '10:00' and '16:00')
OR (CAST(date AS TIME) between '20:00' and '23:59')
OR (CAST(date AS TIME) between '00:00' and '08:00'))