0

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.

4

1 に答える 1

2

In Oracle date arithmetics, subtracting two dates results in the number of days between the two dates:

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?

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:

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).

于 2012-07-25T14:25:04.807 に答える