SQL将输出限制为特定的月份和年份

时间:2017-10-19 15:16:01

标签: sql oracle

我试图编写一份SQL语句,仅显示从四月和五月等特定月份雇用的员工,以及排除在某些年份雇用的员工,但我在网上找不到任何解释的资源这该怎么做。我使用的是Oracle SQL,因此我使用的YEAR和MONTH命令遇到了无效的标识符。这是我到目前为止所做的。

 SELECT EMPLOYEE_ID, 
        SUBSTR(LAST_NAME || ' ' || FIRST_NAME, 1, 25) AS "Full Name", 
        JOB_ID,
        TO_CHAR(last_day(hire_date), 'Month fmDd"st" "of" YYYY') AS "Start Date"
 FROM EMPLOYEES
 WHERE HIRE_DATE NOT IN ('01-01-1992' TO_DATE('12-31-1992'));

2 个答案:

答案 0 :(得分:1)

这有帮助吗?

SELECT EMPLOYEE_ID, SUBSTR(LAST_NAME || ' ' || FIRST_NAME, 1, 25) AS "Full Name",
JOB_ID,
TO_CHAR(hire_date, 'Month fmDd"th" "of" YYYY') AS "Start Date"
FROM EMPLOYEES 
WHERE HIRE_DATE NOT 
BETWEEN TO_DATE('01-01-1992','MM-DD-YYYY') 
    AND TO_DATE('12-31-1992','MM-DD-YYYY') 
AND TO_NUMBER(TO_CHAR(hire_date,'mm')) in (4,5);

答案 1 :(得分:0)

您也可以使用EXTRACT来解决此类情况。

SELECT employee_id,
       "full name",
       "start date"
FROM   (SELECT employee_id,
               Substr(last_name
                      || ' '
                      || first_name, 1, 25) AS
               "Full Name",
               job_id,
               To_char(Last_day(hire_date), 'Month fmDd"st" "of" YYYY') AS
               "Start Date"
                      ,
               Extract (year FROM hire_date)
                      hired_year,
               Extract (month FROM hire_date)
               hired_month
        FROM   hr.employees)
WHERE  hired_year NOT IN ( 1992, 1993 )
       AND hired_month NOT IN ( 4, 5 );