如何正确处理查询约束中的日期 [英] How to correctly handle dates in queries constraints

查看:219
本文介绍了如何正确处理查询约束中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下查询来完成2010年6月的所有检查:

I am currently using the following query to get all the inspections done on june 2010:

select inspections.name
from inspections
where
  to_char(inspections.insp_date, 'YYYY') = 2010 and
  to_char(inspections.insp_date, 'MM') = 06;

但这种感觉有点尴尬。不会有更好的方法这样做吗?查看 http://infolab.stanford.edu/~ullman/fcdb /oracle/or-time.html 它似乎不是。

but this feels kinda awkward. Wouldn't there be a better way of doing this? Looking at http://infolab.stanford.edu/~ullman/fcdb/oracle/or-time.html it doesn't seem so. I am using Oracle, if it makes a difference.

感谢

推荐答案

p>我喜欢使用范围比较,因为这可以用于优化器的索引扫描:

I like to use range comparison when possible since this can be used for index-scan by the optimizer:

select inspections.name
  from inspections
 where inspections.date >= DATE '2010-06-01'
   and inspections.date < DATE '2010-07-01'

这篇关于如何正确处理查询约束中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆