如何比较基于sql中月份字段的两个日期

时间:2016-03-03 19:01:25

标签: sql oracle

如何根据sql中的month字段比较两个日期?

有人可以展示一个示例,显示在经理加入的那几个月加入的员工姓名吗?

我试过这些:

SELECT e_name
FROM emp5 e,
    department1 d,
    manager m
WHERE e.e_id = d.empno
    AND m.deptno = d.deptno
    AND month(e.hiredate) = month(m.hiredate);

SELECT e_name
FROM emp5 e,
    department1 d,
    manager m
WHERE e.e_id = d.empno
    AND m.dep tno = d.deptno
    AND month(e.hiredate) = month(m.hiredate)

SELECT e_name
FROM emp5 e,
    department1 d,
    manager m
WHERE e.e_id = d.empno
    AND m.deptno = d.deptno
    AND dd - mon - yy(e.hiredate, '%m-%y') = dd - mon - yy(m.hiredate, '%m-%y');



SELECT e_name
FROM emp5 e,
    department1 d,
    manager m
WHERE e.eid = d.empno
    AND m.deptno = d.deptno
    AND extract(month FROM e.hiredate) = extract(month FROM m.hiredate);

这是我的emp5表 -

E_ID     E_NAME                   SALARY     HIREDATE 
----- ------------------------- ---------- --------- 
e01   Komal Pandya              13000       02-FEB-15 
e02   Ganesh Patel              20000       01-FEB-15 

1 个答案:

答案 0 :(得分:2)

假设您的日期存储为DATE列,这是从日期中提取月份和年份的两种简单方法:

SQL> select extract(month from sysdate), extract(year from sysdate) from dual;

EXTRACT(MONTHFROMSYSDATE) EXTRACT(YEARFROMSYSDATE)
------------------------- ------------------------
                        3                     2016

SQL> select to_char(sysdate, 'mm/yyyy') from dual;

TO_CHAR
-------
03/2016