获取日期时间值之间的行

时间:2016-12-03 09:08:46

标签: sql sql-server datetime

我有一张如下表格

Id    DateTimeIn                DateTimeOut
1     2016-12-03 09:05:22.123   2016-12-03 09:05:22.123
2     2016-12-03 10:00:00.415   2016-12-03 10:40:00.415
3     2016-12-03 11:04:52.253   2016-12-03 11:35:22.123

我想带来时间范围之间的值。

declare @DateTimeIn date = '2016-12-03 11:00:52.213'
declare @DateTimeOut date = '2016-12-03 11:45:52.213'

输入范围中包含Id 3的行。所以我写下面的查询将该行作为输出。

SELECT * 
FROM TimesheetEntries
WHERE @DateTimeIn BETWEEN DateTimeIn AND DateTimeOut 
   OR @DateTimeOut BETWEEN DateTimeIn AND DateTimeOut

但它没有带来任何结果。我不知道我的查询有什么问题。请帮帮我!

2 个答案:

答案 0 :(得分:1)

确保您的变量的类型为datetime,而您选择的列的类型为datetime

declare @DateTimeIn datetime = '2016-12-03 11:00:52.213'
declare @DateTimeOut datetime = '2016-12-03 11:45:52.213'

SELECT * FROM TimesheetEntries
WHERE @DateTimeIn BETWEEN DateTimeIn and DateTimeOut 
OR @DateTimeOut BETWEEN DateTimeIn and DateTimeOut

答案 1 :(得分:0)

Table of contents:

<div>
  <ul id="toc">
  </ul>
</div>  

___

<h1>TitleA</h1>
<h1>TitleB</h1>
<h2>SubTitleB1</h2>
<h2>SubTitleB2</h2>
<h1>TitleC</h1>
<h1>TitleD</h1>
<h2>SubTitleD1</h2>