vs> = startdate和< = enddate之间的SQL

时间:2011-03-29 18:23:13

标签: sql plsql

我在PL / SQL中编写了一些SQL查询,要求我根据日期过滤记录。该字段是日期/时间类型,但由于我并不真正关心我想到的时间,因此我将从where子句中省略它。

所以我写的是

WHERE
  f.logdate between to_date('2011/01/01', 'yyyy/mm/dd') and
                    to_date('2011/01/31', 'yyyy/mm/dd')

获取1月份的所有记录。我读到这应该等同于

WHERE
  f.logdate >= to_date('2011/01/01', 'yyyy/mm/dd') and
  f.logdate <= to_date('2011/01/31', 'yyyy/mm/dd')

但是我的最终结果并不是我的预期:使用BETWEEN关键字的记录少于我明确说明边界时的记录。是因为我对BETWEEN所做的假设是错误的吗?

编辑:啊nvm,似乎日期不是问题。我正在使用的子查询也是按日期过滤其结果集,并指定日期/时间,而不是。

2 个答案:

答案 0 :(得分:2)

你能否显示“logdate”字段的类型(sql create sentence有帮​​助)?

在某些数据库中,日期类型实际上是日期时间字段,因此如果您在“2011年1月1日”之后查找日期,那么您真的在“2011年1月1日12:00:00 pm”之后查找日期。

可能是你的情况。

答案 1 :(得分:1)

如果时间设置为0:00或类似的东西,它将无法正常工作。

The query retrieves the expected rows because the date values in the query and the datetime values stored in the RateChangeDate column have been specified without the time part of the date. When the time part is unspecified, it defaults to 12:00 A.M. Note that a row that contains a time part that is after 12:00 A.M. on 1998-0105 would not be returned by this query because it falls outside the range.

http://msdn.microsoft.com/en-us/library/ms187922.aspx

相关问题