How can I bring in a select a date range of a period in the oracle database?

Asked

Viewed 69 times

0

Good morning, everyone,

I have a question but I wanted to know if it is possible to make such action.

I have a table with two date fields, start date and end date, I was wondering if it has how to bring in a select without the loop and without block, the days that are within that range?

In the case I refer to the exact dates and not the number of days between the dates.

I thought about creating situations with Unions, but from what I was seeing at the beginning of the query I already realized that it would be unviable.

SELECT START_DATE
FROM HR.JOB_HISTORY
WHERE START_DATE = TO_DATE('28-OCT-01', 'DD-MM-YY')
UNION ALL
SELECT START_DATE + 1
FROM HR.JOB_HISTORY
WHERE START_DATE = TO_DATE('28-OCT-01', 'DD-MM-YY');
SELECT * FROM HR.JOB_HISTORY WHERE START_DATE = TO_DATE('28-OCT-01', 'DD-MM-YY');

I used an oracle base 11g to test, HR.JOB_HISTORY, filtering the record from day 28/10/2001. For this record the start date is 10/28/2001 and end date is 03/15/2005.

Then I thought about creating a with the query below.

with
dias as
(
SELECT CASE WHEN START_DATE > END_DATE THEN TRUNC(START_DATE - END_DATE)
            WHEN END_DATE > START_DATE THEN TRUNC(END_DATE - START_DATE)
            END AS QTD_DIAS,
            START_DATE,
            END_DATE,
       A.* 
FROM HR.JOB_HISTORY A
);

And take to a second query the value of the number of days, which would be the reference of the limit of records that should come in select, limiting through ROWNUM.

SELECT * 
FROM HR.JOB_HISTORY 
WHERE START_DATE = TO_DATE('28-OCT-01', 'DD-MM-YY')
      AND ROWNUM <= DIAS.QTD_DIAS;

But it also would not be feasible because it would bring only the record that fall in the Where condition.

By the way, thinking here I realized that maybe there is no way to get the result without the loop and without the pl sql block, by the block with the loop I did not get to do but I believe that it works, my doubt is more if only with the pl is possible. If there is any solution it will be of great help.

  • Luiz, have you tried anything? Ask the question.

  • I did Rodrigo, I will pass on the most evident queries in a new answer because in the comments the character limit is exceeded.

  • OK Felipe, edit the question and insert it into it.

  • Rodrigo, I’ve adjusted the question with the ladies.

  • Exchange TO_DATE('28-OCT-01', 'DD-MM-YY') for TO_DATE('28-10-2001', 'DD-MM-YYYY') first of all.

No answers

Browser other questions tagged

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