针对日期列的 Oracle SQL Where 子句
我有一个包含日期的 DATE 列,但我需要查询它以查找不到 30 天的记录。
START_DATE
----------
01-AUG-2010
09-AUG-2010
22-AUG-2010
09-SEP-2010
查询:
SELECT START_DATE
WHERE START_DATE < 30;
我知道这是 ORACLE SQL 中的简单查询,但我做错了。
I have a DATE column with a date in it but I need to query it to find records less than 30 days old.
START_DATE
----------
01-AUG-2010
09-AUG-2010
22-AUG-2010
09-SEP-2010
Query:
SELECT START_DATE
WHERE START_DATE < 30;
I know it is simple Query in ORACLE SQL but i am doing something wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用:
如果要根据截至午夜的 30 天计算日期,请使用 TRUNC 函数:
不要在列上运行 TRUNC - 这将使列上的索引变得无用,从而确保表扫描。
Use:
SYSDATE
is Oracle's syntax to get the current date and timeIf you want to evaluate the date based on thirty days as of midnight, use the TRUNC function:
Don't run TRUNC on the column - that will render an index on the column useless, ensuring a table scan.
尽管我注意到 Oracle 和 PostgreSQL 之间的 INTERVAL 语法存在一些细微差别,但 INTERVAL 比假设您可以添加或减去天数更可移植。
INTERVAL
is more portable than assuming that you can add or subtract days, although I've noticed some slight differences in theINTERVAL
syntax between Oracle and PostgreSQL.哪里 START_DATE > SYSDATE - 1
或者
WHERE TRIM(STARTDATE) >修剪(系统日期)- 1
WHERE START_DATE > SYSDATE - 1
or perhaps
WHERE TRIM(STARTDATE) > TRIM(SYSDATE) - 1