Home » SQL & PL/SQL » SQL & PL/SQL » Determine all data between 2 dates
Determine all data between 2 dates [message #645317] Thu, 03 December 2015 02:35 Go to next message
parawos
Messages: 3
Registered: December 2015
Junior Member
Hello,

i have folowing problem. In my database I have several intervalls:

1. Table
validfrom validto
01.01.15 - 02.02.15
02.02.15 - 15.03.15
15.03.15 - ....

I have to select for a given date intervall, for example from 03.02.15 until 16.03.15 all results from 1. Table. End result should be:

validfrom validto
03.02.15 - 15.03.15
15.03.15 - 16.03.15

I am not an expert in PL/SQL, that I tried to use between function...but I didn't achieve my result. Could you please help me?
Re: Determine all data between 2 dates [message #645319 is a reply to message #645317] Thu, 03 December 2015 02:51 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You have to return all rows where validfrom is lower than your first date but with with validto greater then this one, and validto is greater than your second date but with validfrom lower than this one.
There is no need of function (but maybe TO_DATE is your inputs are strings) just OR and AND.

[Updated on: Thu, 03 December 2015 02:52]

Report message to a moderator

Re: Determine all data between 2 dates [message #645320 is a reply to message #645319] Thu, 03 December 2015 02:57 Go to previous messageGo to next message
parawos
Messages: 3
Registered: December 2015
Junior Member
ok, and how can I set the start date of interval by my given from date(03.02.15) and determine it by my todate(16.03.15).
Re: Determine all data between 2 dates [message #645323 is a reply to message #645320] Thu, 03 December 2015 03:24 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Applying what I said in the WHERE clause.

Re: Determine all data between 2 dates [message #645324 is a reply to message #645323] Thu, 03 December 2015 03:30 Go to previous messageGo to next message
parawos
Messages: 3
Registered: December 2015
Junior Member
select *
          from example  a 
          where
        ((a.VALIDFROMDATE between TO_DATE ('01.12.28', 'dd.mm.yy') AND TO_DATE ('01.01.29', 'dd.mm.yy'))
      or (a.VALIDTODATE between TO_DATE ('01.12.28', 'dd.mm.yy') AND TO_DATE ('01.01.29', 'dd.mm.yy')
      or ((a.VALIDFROMDATE <= TO_DATE ('01.12.28', 'dd.mm.yy')) AND a.VALIDFROMDATE is null)))
         
         order by a.VALIDFROMDATE desc;

I have now this sql statement. All results between '01.12.28' and '01.01.29'.
Re: Determine all data between 2 dates [message #645325 is a reply to message #645324] Thu, 03 December 2015 03:33 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Maybe you have what you say maybe not, we can't say as we have neither your table nor your data nor your session.

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Also always post your Oracle version, with 4 decimals.

With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data.

Use SQL*Plus and copy and paste what you already tried.

Previous Topic: SQL update
Next Topic: Grouping with sum and retain first row
Goto Forum:
  


Current Time: Thu Apr 25 07:57:23 CDT 2024