Return Anniversary of Hiring Weekly in SQL Oracle

Asked

Viewed 51 times

0

I need to provide information on the hiring anniversaries per week. Exemplifying once a week HR will send a message of congratulations to the hiring birthday.

I created the following SQL query:

SELECT

    HIRE_DATE AS DATA_CONTRATACAO,
    EXTRACT(DAY FROM HIRE_DATE) AS DIA_CONTRATACAO,
    EXTRACT(MONTH FROM HIRE_DATE) AS MES_CONTRATACAO,
    SYSDATE AS HOJE,
    SYSDATE+7 AS DATA_LIMITE


FROM
    EMPLOYEES

WHERE EXTRACT(MONTH FROM HIRE_DATE) = EXTRACT(MONTH FROM SYSDATE) 
        AND EXTRACT(DAY FROM HIRE_DATE) >= EXTRACT(DAY FROM SYSDATE+7)

Retorno Consulta

There is a logic error because only the separate days we lose the month reference as in the example bettwen.

AND EXTRACT(DAY FROM HIRE_DATE) >= EXTRACT(DAY FROM SYSDATE+7)

  • One possibility is to use a field calculated in the WHERE clause: WHERE EXTRACT(MONTH FROM HIRE_DATE)*100 + EXTRACT(DAY FROM HIRE_DATE) BETWEEN EXTRACT(MONTH FROM SYSDATE)*100 + EXTRACT(DAY FROM SYSDATE) AND EXTRACT(MONTH FROM SYSDATE)*100 + EXTRACT(DAY FROM SYSDATE+7), or use week with the function DATEPART.

  • Why multiplied by 100 ?

  • Create a Month field to compare.

1 answer

0


Just change the logic of yours where.

Convert the date to the current year thus:

TO_DATE(TO_CHAR(hire_date,'DD/MM'),'DD/MM')

and then compare the dates using a between:

TO_DATE(TO_CHAR(hire_date,'DD/MM'),'DD/MM') BETWEEN TRUNC(SYSDATE) AND TRUNC(SYSDATE + 7)

See spinning here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.