我需要使用包含名为“ CreatedDate”的字段的数据来生成报告。此字段是日期和时间戳。我需要创建一个查询,该查询的操作类似于选择CreatedDate从'01 / 01/2018'到'06 / 02/2018'的所有行,并且仅在时间介于'13:30:00'到'18:00:00'。我在尝试完成此操作时遇到了问题。
下面是我尝试过的一个示例:
SELECT
*
FROM [tablename]
WHERE [CreatedBy] = '792'
AND ([CreatedDate] >= '2018-01-01'
OR [CreatedDate] <= CAST(GETDATE() AS DATE))
AND ([CreatedDate] BETWEEN '13:30:00' AND '16:00:00')
ORDER BY
[ID] DESC;
现在这行不通了,但是不确定如何做到这一点。任何帮助将不胜感激。 -谢谢-
答案 0 :(得分:3)
尝试使用TIME函数。
select * from [tablename]
where CreatedBy = '792'
and (CreatedDate >= '2018-01-01' or CreatedDate <= CAST(GETDATE() as DATE)) AND
(TIME(CreatedDate) BETWEEN TIME('13:30:00') AND TIME('16:00:00')) ORDER BY ID desc
答案 1 :(得分:2)
尝试一下select * from [tablename]
where CreatedBy = '792'
and (CreatedDate >= '2018-01-01' or CreatedDate <= CAST(GETDATE() as DATE)) AND
(CONVERT(VARCHAR(8),CreatedDate ,108) BETWEEN '13:30:00' AND '16:00:00' ORDER BY ID desc
答案 2 :(得分:0)
假设:
(CreatedDate BETWEEN '13:30:00' AND '16:00:00') ORDER BY ID desc
是无效的部分-您可以尝试:
and (DATEPART(Hour, CreatedDate) between 12.5 and 15