Query between two date columns ?

Oracle 11g R2
I'm trying too create a query between two date columns. I have a view that consists of many columns. There are two columns in question called valid_to and valid_from
Part Number     Valid_from        valid_to
100                    01/01/2000       01/01/9999
200                    01/01/2000       01/01/9999
300                    01/01/2000       01/01/9999
etc
If I want to only see rows between with a date range of 01/01/2000 and 01/01/2013 how can I put this as SQL ?
Thanks in advance

Hi,
Whenever you have a problem, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), so that the people who want to help you can re-create the problem and test their ideas.
Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
See the forum FAQ: https://forums.oracle.com/message/9362002
If you want to find rows that have that exact range, then you can do  something like
SELECT  *
FROM    table_x
WHERE   valid_from  = DATE '2000-01-01
AND     valid_to    = DATE '2013-01-01'
If you want to find rows where any or all or the range on the row overlaps any or all of the 200-2013 target range, then
SELECT  *
FROM    table_x
WHERE   valid_from  <= DATE '2013-01-02
AND     valid_to    >= DATE '2000-01-01'
If you want rows that are enritely within the target range, it's something else.
If you want rows that entirely enclose the target range, it's something else again.

Similar Messages

  • Select Query Between two dates...

    Hi Guru's,
    I need a Select Query between two dates, also if the record not found for any in between date then it should return NULL or 0 ...
    for Example
    1. I am having two records in DB for date 2-10-2008 & 4-10-2008
    2. Now suppose I have given Query for date between 1-10-2008 to 5-10-2008
    Then it should return me 5 records with valid values for 2 & 4 and NULL for other 1,3,5
    Thanks.

    Try like this:
    with
      t as
          select date '2008-10-02' as dt, 'Record #1 (in DB)' as str from dual union all
          select date '2008-10-04' as dt, 'Record #2 (in DB)' as str from dual
    select v.dt, t.str
      from (
             select date '2008-10-01' + level - 1 as dt
               from dual
             connect by level <= (date '2008-10-05' - date '2008-10-01') + 1
           ) v
      left join t
        on v.dt = t.dt
    order by 1

  • SQL Query between two dates

    Hi,
    Please could someone help me on how to write a query to retrieve the data between two dates.
    created_date format stored in the database column: 9/18/2007 11:34:03 AM
    I tried below but it didn't work
    select * from work_table where created_date beween '9/18/2007' and '03/29/2008'
    I get 'literal doesn't match format string'
    Thanks,

    date datatype is nls dependent -> folllows the nls_date_format database parameter setting inherited by your session.
    Making it short you'll be always safe if you
    select * from work_table where created_date beween to_date('9/18/2007','MM/DD/YYYY') and to_date('03/29/2008','MM/DD/YYYY')Having the time component included you must add + 1 - 1 / 24 / 60 / 60 (plus one day minus one second) to the upper limit if you want to include the last day as a whole
    *** not tested
    Regards
    Etbin

  • Query between two dates

    Hi,
    I have table like this.
    Id           in_date               value
    2          05-Jun-08          5.3
    3          08-Jun-08          5.2
    4          08-Jun-08          5.3
    5          08-Jun-08          5.8
    6          10-Jun-08          7
    7          10-Jun-08          5.6
    8          11-Jun-08          2.6
    When I query for average value between two dates, it should return average and if there is no entry for any dates it should return 0 for that date. Here is sample output for dates between 05-Jun-08 and 12-Jun-08.
    In_date           value
    05-Jun-08     5.3
    06-Jun-08     0.0
    07-Jun-08     0.0
    08-Jun-08     5.43
    09-Jun-08     0
    10-Jun-08     6.3
    11-Jun-08     2.6
    12-Jun-08     0
    Please help me to write query on thisThanks,
    Sujnan

    SQL> with t as (
      2             select 2 id,to_date('05-Jun-08','dd-mon-rr') in_date,5.3 val from dual union all
      3             select 3,to_date('08-Jun-08','dd-mon-rr'),5.2 from dual union all
      4             select 4,to_date('08-Jun-08','dd-mon-rr'),5.3 from dual union all
      5             select 5,to_date('08-Jun-08','dd-mon-rr'),5.8 from dual union all
      6             select 6,to_date('10-Jun-08','dd-mon-rr'),7 from dual union all
      7             select 7,to_date('10-Jun-08','dd-mon-rr'),5.6 from dual union all
      8             select 8,to_date('11-Jun-08','dd-mon-rr'),2.6 from dual
      9            )
    10  select  t2.in_date,
    11          nvl(t1.val,0) + t2.val val
    12    from  (
    13           select  in_date,
    14                   avg(val) val
    15             from  t
    16             group by in_date
    17          ) t1,
    18          (
    19           select  min_in_date + level - 1 in_date,
    20                   0 val
    21             from  (
    22                    select  min(in_date) min_in_date,
    23                            max(in_date) max_in_date
    24                      from  t
    25                   )
    26             connect by level <= max_in_date - min_in_date + 1
    27          ) t2
    28    where t2.in_date = t1.in_date(+)
    29    order by t2.in_date
    30  /
    IN_DATE      VAL
    05-JUN-08   5.30
    06-JUN-08    .00
    07-JUN-08    .00
    08-JUN-08   5.43
    09-JUN-08    .00
    10-JUN-08   6.30
    11-JUN-08   2.60
    7 rows selected.
    SQL> SY.

  • Difference between two date columns

    Dear professionals,
    how to get different between two columsn (sati_do-sati_od) in format hh24:mi:
    select to_char(sati_od,'hh24:mi') "hh:mi format",
    to_char(sati_do,'hh24:mi') "orginal_value_col2",
    sati_od "original_value_col1",
    sati_do "original value",
    sati_do-sati_od "Difference"
    from accbtp_pliskljucenjaodobrenja
    where idplisklj= 22999;where result is:
    hh:mi format orginal_value_col2 original_value_col1       original value            Difference            
    09:00        15:00              01.12.10                  01.12.10                  0,25                  
    09:00        15:00              01.12.10                  01.12.10                  0,25                  
    09:00        15:00              01.12.10                  01.12.10                  0,25                  
    09:00        15:00              01.12.10                  01.12.10                  0,25                   Thx in advance, Adnan

    Mahanam wrote:
    select trunc( mod( (e-j)*24, 24 ) ) ||':'||trunc( mod( (e-j)*24*60, 60 ) )  "Hr:Mi"
    from (
    select t.*,
    trunc(sysdate,'y')+jd+jh/24+jm/24/60 j,
    trunc(sysdate,'y')+ed+eh/24+em/24/60 e
    from t
    ) t
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:96012348060
    Thx for answering, this works now !
    select to_char(sati_od,'hh24:mi') "hh:mi format",
    to_char(sati_do,'hh24:mi') "orginal_value_col2",
    sati_od "original_value_col1",
    sati_do "original value",
    sati_do-sati_od "Difference",
    trunc( mod( (sati_do-sati_od)*24, 24 ) ) ||':'||trunc( mod( (sati_do-sati_od)*24*60, 60 ) )  "Hr:Mi"
    from accbtp_pliskljucenjaodobrenja
    where idplisklj= 22999;result is
    hh:mi format orginal_value_col2 original_value_col1       original value            Difference             Hr:Mi                                                                            
    09:00        15:00              01.12.10                  01.12.10                  0,25                   6:0                                                                              
    09:00        15:00              01.12.10                  01.12.10                  0,25                   6:0                                                                              
    09:00        15:00              01.12.10                  01.12.10                  0,25                   6:0                                                                              
    09:00        15:00              01.12.10                  01.12.10                  0,25                   6:0                                                                              

  • How to calculate the month difference between two date char. in Query?

    Customers would like to see how many months passed between two date type of characteristics (e.g., the month difference between the current date and the scheduled delivery date in the record) and put the result into the column as KF. 
    We would have to grab the fiscal year/period kind of value and then do the subtraction, e.g., if the current date value is 2/28/2008 and the scheduled delivery date value in the record is 12/01/2007, the correct result should be 2 month difference between these two date values, but could someone here give us the technical light on how to make this happen in query design?
    Thanks and we will give you reward points for the correct anwsers!

    Hi Kevin,
    The Badi is RSR_OLAP_BADI.
    You can create an implementation using Transaction  SE18.
    The implementation is per cube and is defined in the filters.
    In the Implementation you have the following methods :
    1. Define : Here you will provide the Keyfigure you need as a virtual one.
    2. Initilialize : Any Init Function you want to do.
    3. Compute. This is called per datarecord and here you can cimpute your value.
    Hope this helps.
    Pralay Ahluwalia

  • Query for between two dates.

    Hi,
    I've a column of date, and I want to select the records between two dates, the output of this columns is: 08/19/2003 2:11:00 AM
    My query is:
    SELECT DTM FROM ITEM
    WHERE DTM BETWEEN '07/01/2012' AND '07/15/2012'
    Please help.
    Thanks,

    >
    I've a column of date, and I want to select the records between two dates, the output of this columns is: 08/19/2003 2:11:00 AM
    My query is:
    SELECT DTM FROM ITEM
    WHERE DTM BETWEEN '07/01/2012' AND '07/15/2012'
    Please help.
    >
    Help with what? You didn't ask a question or indicate if you are having a problem of some sort.
    Did you need to know how to use TO_DATE instead of literals in the WHERE clause?
    SELECT DTM FROM ITEM
    WHERE DTM BETWEEN TO_DATE('07/01/2012', 'MM/DD/YYYY') AND TO_DATE('07/15/2012', 'MM/DD/YYYY');

  • How to query the number of working days between two dates

    I'm looking for a solution to calculate the number of <i>working</i> days between two dates that I can use in a formated search. 
    Calculating the total number of days is pretty straight forward but does anyone know how to take into account the settings in the HLD1 (Holiday Dates) table?

    Hi Eric,
    If you are purely looking to exclude holidays defined in the HLD1 table, then you should be able to do it with the following query
    NOTE: The following query is an example using OINV table and the fields DOCDATE and DOCDUEDATE for a Particular DOCNUM  'xxx'
    If you planning to use within the SAP module then replace DOCDATE and DOCDUEDATE with dynamic field references $[$x.x.x]
    SELECT DATEDIFF(DAY,T0.DOCDATE,T0.DOCDUEDATE)-
    (SELECT COUNT(STRDATE) FROM HLD1 WHERE STRDATE >= T0.DOCDATE AND STRDATE <= T0.DOCDUEDATE)
    FROM OINV T0
    WHERE T0.DOCNUM = xxx
    Best Wishes
    Suda

  • Difference between two date in bex query

    Hi all,
    I need to do a difference between two date using formula variable processing type customer exit beaucause I must use factory calendar in the formula.
    How can I do it?
    Can you give me an example of the routine?
    Thanks a lot
    Gianmarco

    Hi,
    You can still use the same code to copy it and customize as per your need. All you need to do is to subract the dates using the class: CL_ABAP_TSTMP after converting to timestamp and resulting seconds you convert back to days....Please get help from the developers to do this...
    Also, ensure that you write back this difference value to your variable so you get it on the reports for your calculations...
    Cheers,
    Emmanuel.

  • How to Calculate number of months between two dates

    Hi All,
       In one of the fomr developments, I have to calculate the
    Number of Days
    Number of Months ( Considering Leap Year) provided by the dates, end user enters in the form,
    After going thorugh some forum discussion, I have come to know about so many things which were not clear till now.
    I have gone through various forums too,  some one suggets to make use of FORM CALC and some other JAVA SCRIPT. But the logic i want to build in java script.
    The most interesting point is the DATE object is not getting created when i write  the below code
      var startDate = new DATE(oYear, oMonth, oDay);
    I am still not clear, that really the date object gets created in Adobe form If so the why the alert box is getting populated when i write below lines
    var oTemp = startDate.getFullYear();
    xfa.host.messagebox(oTemp);
    So, there are so many unclear things,
    If any one can help me by suggesting the approach and how to build the logic in the JavaScript I would be really thankful
    Regards
    PavanChand

    Hi,
    ChakravarthyDBA wrote:
    Hi
    I want number of Sundays between two dates
    example
    number of Sundays count between '01-04-2013' and '30-04-2013' in one select query I have to include this as sub query in my select statement.Here's one way:
    SELECT       early_date
    ,       late_date
    ,       ( TRUNC (late_date + 1, 'IW')
           - TRUNC (early_date,        'IW')
           ) / 7       AS sundays
    FROM       table_x
    ;This does not depend on your NLS settings.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data.
    Point out where the statment above is getting the wrong results, and explain, using specific examples, how you get the right results from the given data in those places.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}

  • How to count number of sundays between two dates

    Hi
    I want number of Sundays between two dates
    example
    number of Sundays count between '01-04-2013' and '30-04-2013' in one select query I have to include this as sub query in my select statement.

    Hi,
    ChakravarthyDBA wrote:
    Hi
    I want number of Sundays between two dates
    example
    number of Sundays count between '01-04-2013' and '30-04-2013' in one select query I have to include this as sub query in my select statement.Here's one way:
    SELECT       early_date
    ,       late_date
    ,       ( TRUNC (late_date + 1, 'IW')
           - TRUNC (early_date,        'IW')
           ) / 7       AS sundays
    FROM       table_x
    ;This does not depend on your NLS settings.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data.
    Point out where the statment above is getting the wrong results, and explain, using specific examples, how you get the right results from the given data in those places.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}

  • Select Between two date ranges from xml file

    Hi ,
    I have a column date_ with datatype VARCHAR2(150) it stores
    data as
    05/19/2010 11:23 AM
    05/20/2010 12:23 PM
    05/22/2010 11:23 AM
    05/25/2010 11:23 AM
    i have to select all the rows between 05/19/2010 and 05/22/2010 how to do that this column is in xml file

    I have a table wit two fields
    Field1 is integer and field2 is xmltype
    in the xmltype i store an xml file
    <ParentNode>
    <Node>
    <Cat>1</Cat>
    <Date>05/19/2010 11:23 AM </Date>
    </Node>
    <Node>
    <Cat>2</Cat>
    <Date>05/20/2010 12:23 PM </Date>
    </Node>
    <Node>
    <Cat>3</Cat>
    <Date>05/22/2010 11:23 AM </Date>
    </Node>
    </Parentnode>
    I am using teh below query to retrive teh result
    SELECT T.Feild1, XML.* FROM Tablename T,
    XMLTable( 'Parentnod/Node' PASSING T.Feild2 COLUMNS Cat NUMBER PATH 'Cat' ,
    DATE_ VARCHAR2(100) PATH 'Date'
    )XML where cat >1;
    now i have to do teh same to select the rows between two date range 05/19/2010 and 05/21/2010
    hope i am able to make teh question simple

  • Count days between two dates without weekend

    Hi,
    I need a solution in query or another thread, that returns the count of days between two dates without consider weekend (saturday and sunday) , I have the columns of type Date, and the return need in format of hours in one column hh:mm:ss and days in another column.
    Regards
    Jonas

    Hi and welcome to the forum.
    Keep in mind that you can do a search on this forum.
    Your question has been asked before.
    Some other pointers:
    http://asktom.oracle.com/pls/asktom/ASKTOM.download_file?p_file=6551242712657900129
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:185012348071

  • Findig dates between two dates

    Hi everybody,
    I have one table (TEST) with 2 columns having only one record
    i need list of dates between from_date and to_date
    the sample data...
    from_date to_date
    ====== ======
    02-JUL-09 17-JUL-2009
    I found below query to retrieve the dates between two dates.
    select to_date('02-jul-2009','dd-mon-yyyy')+level-1 dt from dual
    connect by level<=to_date('17-jul-2009','dd-mon-yyyy')-to_date('02-jul-2009','dd-mon-yyyy')+1
    It is working properly.
    i have changed the above query with my table column names
    select to_date(from_date,'dd-mon-yyyy')+level-1 dt from Test
    connect by level<=to_date(to_date,'dd-mon-yyyy')-to_date(from_date,'dd-mon-yyyy')+1
    In this case it is not working...
    i am unable to find the reason...
    Thank you.

    Hi,
    It is working fine for me, what is the data type for from_date and to_date.
    select to_date(from_date,'dd-mon-yyyy')+level-1 dt from Test
    connect by level<=to_date(to_date,'dd-mon-yyyy')-to_date(from_date,'dd-mon-yyyy')+1;
    DT
    02-JUL-09
    03-JUL-09
    04-JUL-09
    05-JUL-09
    06-JUL-09
    07-JUL-09
    08-JUL-09
    09-JUL-09
    10-JUL-09
    11-JUL-09
    12-JUL-09
    13-JUL-09
    14-JUL-09
    15-JUL-09
    16-JUL-09
    17-JUL-09Regards
    Anurag Tibrewal
    PS: Also post your output with the second query.

  • How can i get report between two dates?

    Hi
    how can i get report between two dates?
    for example i want get reports between 20/4/2002 & 27/4/2002.
    my table has date column and i can get first date( exam : .... where date:=a and/or ....i don't know this part)
    thanks alot.
    Regards
    The Oracle Reports Team
    http://otn.oracle.com/

    where exam_date between :from_date and :to_date
    from_date and to_date are user_parameter

Maybe you are looking for

  • Error connecting SAP BW7.00/BI7.03 to R/3 4/7/6.20

    We recently installed SAPNW2004s from ERP2005 package with BI7.03 addon. Right now we are configuring the system to connect to R/3 system which is a 4.7 appl with 6.20 basis. I am getting below error on RFC connection setting via RSA1 t-code. "RFC us

  • Problem with CS4 activation using student users

    Hi, I am working at a school and over the last few weeks we have rebuilt our network from a 2003/xp to 2008R2/Win7 network so have had to remake a lot of software packages. We have created and deployed CS4 with success across the network but seem to

  • Looks like a bug in Date class

    Hi guys, I just logged in to highlight what I encountered when I was trying to convert milliseconds of a time stamp to a date/time string. Just see the following codes. public function convertTimeStampToDateAndTime(timeStampInMilliseconds:Number):Str

  • Use checkbox to select rows in a af:table

    How to use a checkbox on the first column of a table as row selection control? Just like most of the web email client (Yahoo, Hotmail...). I am using JDeveloper 11.1.1.2 with Fusion Web Application - ADF. The control I am using is a <af:table>. Thank

  • Firefox won't delete cookies even after set to clear cookies on firefox exit

    I have use custom settings for remembering history in firefox. I set to clear history on firefox close. Under this settings, I set to clear cookies, active login and cache. But whenever firefox exits it doesn't remove cookies. Also, I set to keep coo