如何使用 oracle 内联查询从 sysdate 中减去日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11651708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to minus date from sysdate using oracle inline query?
提问by user968441
I am creating one query. In that query i want minus date from system date. My table is like below.
我正在创建一个查询。在那个查询中,我想从系统日期减去日期。我的桌子如下。
Table Name : Employee
---------------------
ID
NAME
JOINDATE
I am writing this query to fetch my result
我正在编写此查询以获取我的结果
SELECT * FROM SS.EMPLOYEE WHERE (sysdate - JOINDATE)='05-Ded-11'
This throws the following error:
这会引发以下错误:
SQL Error: ORA-00920: invalid relational operator
So how to do this???If anyone knows than please help me.
那么如何做到这一点???如果有人知道比请帮助我。
回答by Vincent Malgrat
In Oracle date arithmetics, subtracting two dates results in the number of days between the two dates:
在 Oracle 日期算术中,两个日期相减的结果是两个日期之间的天数:
SQL> SELECT DATE '2000-12-31' - DATE '2000-01-01' year FROM DUAL;
YEAR
----------
365
Your WHERE
clause is therefore incorrect to Oracle since it cannot compare a number to a date. What exactly do you want to express with this comparison?
WHERE
因此,您的子句对 Oracle 不正确,因为它无法将数字与日期进行比较。你到底想通过这个比较来表达什么?
If you want to filter all dates before a given date, just compare the two dates:
如果要过滤给定日期之前的所有日期,只需比较两个日期:
SELECT * FROM SS.EMPLOYEE WHERE joindate <= to_date('2011-12-05', 'yyyy-mm-dd')
If you want to select all rows that are in a given interval, you can use multiple comparisons or the operator BETWEEN
:
如果要选择给定间隔内的所有行,可以使用多重比较或运算符BETWEEN
:
SELECT *
FROM SS.EMPLOYEE
WHERE joindate BETWEEN to_date('2011-12-05', 'yyyy-mm-dd') AND SYSDATE
Update
更新
The number of months between two dates is given by the function MONTHS_BETWEEN
. You would compare the result of this function to a number (of months).
两个日期之间的月数由函数给出MONTHS_BETWEEN
。您可以将此函数的结果与(月数)进行比较。