Query to Identify every Wednesday between 2 dates

Hi,
I have this code that is working well but i found it very ugly.  Any optimisation hints or simplifications?
select to_date(:startdate, 'YYYY-MM-DD') + rownum -1 as Date_
      from all_objects
     where rownum <=  to_date(:enddate, 'YYYY-MM-DD')-to_date(:startdate, 'YYYY-MM-DD')+1
having to_char(to_date(:startdate, 'YYYY-MM-DD') + rownum -1,'D')='4'   --4 = Wednesday
group by to_date(:startdate, 'YYYY-MM-DD') + rownum -1
order by to_date(:startdate, 'YYYY-MM-DD') + rownum -1
Thanks
Hugo

Hi, Hugo,
Here's one way:
WITH  parameters AS
    SELECT  TO_DATE (:start_date, 'YYYY-MM-DD')  AS start_date
    ,       TO_DATE (:end_date,   'YYYY-MM-DD')  AS end_date
    FROM    dual
SELECT  start_date + LEVEL - 1 AS dt
FROM    parameters
WHERE   TO_CHAR ( start_date + LEVEL - 1
                , 'fmDY'
                , 'NLS_DATE_LANGUAGE=ENGLISH'
                )   = 'WED'
CONNECT BY  LEVEL  <= 1 + end_date - start_date
The main thing is to lose the GROUP BY.  GROUP BY is for taking groups of input rows, and producing 1 one output row from them.  You're not doing anything like that in this problem.
Instead of repeating a function call multiple times in the same query (e.g., you used
to_date(:startdate, 'YYYY-MM-DD')
5 times in the query you posted) , you can do it once, in a sub-query, and use the result as many times as you need to in a super-query.
CONNECT BY from a 1-row table (or, in this case, result set) is much more efficient than getting ROWNUM from all_objects.  Also, there is a finite number of rows in all_objects.  That number is probably in the thousands, but it's still a limit.  CONNECT BY will work for millions, or more, if you need it.
I find TO_CHAR (some_date, 'DY') easier to debug and maintain than TO_CHAR (some_date, 'D').  I might get confused, and think that '3' means Wednesday, but I'm not very likely to think that 'TUE' means Wednesday, no matter how little coffee I've had.
Also, the 'D' format depends on your NLS_TERRITORY setting, which you can't control from within the query. 'DY' depends on NLS_LANGUAGE, which you can override by passing a 3rd argument to TO_CHAR.  The query above will always check for Wednesdays, even if they are called Onsdag or Mittwoch in your session, and regardless of whether it thinks that's the 3rd or the 4th day of the week.

Similar Messages

  • How to get LASTDAY for each and every month between given dates..

    Hi Friend,
    I have a doubt,How to get LASTDAY for each and every month between given dates..
    for ex:
    My Input will be look like this
    from date = 12-01-2011
    To date = 14-04-2011
    And i need an output like
    31-01-2011
    28-02-2011
    31-03-2011
    is there any way to achieve through sql query in oracle
    Advance thanks for all helping friends

    Here's a 8i solution :
    select add_months(
             trunc(
               to_date('12-01-2011','DD-MM-YYYY')
             ,'MM'
           , rownum ) - 1 as results
    from all_objects
    where rownum <= ( months_between( trunc(to_date('14-04-2011','DD-MM-YYYY'), 'MM'),
                                      trunc(to_date('12-01-2011','DD-MM-YYYY'), 'MM') ) );
    The above two query is worked in oracle 11GActually the first query I posted is not correct.
    It should work better with
    months_between(
       trunc(to_date(:dt_end,'DD-MM-YYYY'),'MM'),
       trunc(to_date(:dt_start,'DD-MM-YYYY'),'MM')
    )Edited by: odie_63 on 12 janv. 2011 13:53
    Edited by: odie_63 on 12 janv. 2011 14:11

  • URGENT..Date Query..How to Query Records created between 2 dates..

    Hi,
    I want to Query records which are created between 2 dates.
    I can use trunc(creation_date) this will give all the records for that day.
    I have to run a program every 15-20 minutes.
    following is an example
    example
    sysdate = 1/19/03 1:03:42 PM
    record1 creation date = 1/17/03 11:54:10 AM
    record2 creation date = 1/17/03 11:53:49 AM
    record3 creation date = 1/17/03 11:53:08 AM
    last resuest_date = 1/17/03 10:00:08 AM
    now i have to query records created between
    sysdate and last_request_date
    please reply ASAP
    Thanks
    Arun

    SELECT TO_CHAR(creation_date, 'DD-MON-YYYY') creation_date
    FROM navneet
    WHERE TO_CHAR(TO_DATE(creation_date, 'DD-MON-YYYY'), 'DD-MON-YYYY') BETWEEN
    TO_CHAR(TO_DATE('17-JAN-2003', 'DD-MON-YYYY'), 'DD-MON-YYYY') AND
    TO_CHAR(TO_DATE('20-JAN-2003','DD-MON-YYYY'),'DD-MON-YYYY');

  • Stablish conditions between a data field in a query

    Hello all,
    Firstly, I´ve got an infoCube "A" where every record has a field called "Init date" (date field) and another field called "finish date"
    In a query (that retrieves data for the infoCube A) there is a pop up with a date that  user must enter (interval). This query must show every record for which  different conditions regarding interval date are carried out by init date and finish date.
    For example:
    User enter:      03.02.2007   --    17.04.2007
    Query must show every record where init date is minor than 03.02.2007 and finish date is greater or equal than 03.03.2007 and greater than 17.04.2007.
    I think this must be done in BW exit but I don´t know abap code I must enter.
    Anybody can help me?
    Thanks a lot.
    Carmen

    If you want to restrict data for sertain period, and these values are entered in variable entry screen. Query will display only that data which satisfies the entered conditions.
    What exactly you want ? Is,t this your requirement ?
    Regards
    SS

  • Selecting values between 2 dates for an SQL query report region

    I have a region where im selecting between 2 dates
    select hp.NAME, hp.VALUE, hp.UPDATEDATE, hp.DESCRIPTION, hp.UPDATE_COMMENT, hp.HASH, hp.IDCATEGORY
    from HIST_REPORT hr, HIST_PARAM hp
    where hp.dbid = hr.dbid and hr.id = :P7_ID and hp.UPDATEDATE between :P7_BDATE and :P7_EDATE
    The problem is that if I have a row thats dated today (april 1) and if I set the beginning date (BDATE) to april 1 as well as the ending date (EDATE), no rows show and I would like the april 1 row to show. How can I modify my query to fix this?

    and hp.UPDATEDATE between :P7_BDATE
        and decode(:P7_EDATE,:P7_BDATE,:P7_EDATE+1,:P7_EDATE)This assumes, though, that the time component of your items is implicitly 00:00.
    Joel

  • 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

  • ABAP Query: creation of local field (Subtraction between 2 dates)

    Hi,
    I want to create a local field which will calculate the number of days between 2 dates (for example, document date - posting date).
    What I did:
    1) Created the user group in SQ03
    2) Created the infoset in SQ02
    3) I'm stucked at the creation of the Query in SQ01. I'm at the «Create Query: Select field screen». I clicked on Edit --> Local Field --> Create. I selected the «calculation field» attribute. I clicked on the «Complex calculation button», but I don't know how to write my formulate DOCUMENT DATE - POSTING DATE.
    Thanks a lot for your help.
    Julien

    Try these FM's to calculate difference between two dates..
    DAYS_BETWEEN_TWO_DATES
    LEAP_DAYS_BETWEEN_TWO_DATES
    HR_SEN_CALE_DAYS_DATE
    ISB_DAYS_BETWEEN_TWO_DATES
    FIMA_DAYS_BETWEEN_TWO_DATES
    FIMA_DAYS_BETWEEN_TWO_DATES_2
    FIMA_LEAP_DAYS_BETWEEN_2_DATES
    SD_DATETIME_DIFFERENCE

  • 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

  • Identify a specific day between two dates

    I’m look to identify Friday between to dates. Example: start date 12/23/2014 end date 12/30/14 find 12/26/2014 which is a Friday. Or start 1/10/2015 end date 1/17/2015 find 1/16/2015.

    Let's say you have the start date in cell A1. In another cell, enter the formula
    =A1+MOD(6-A1,7)
    Format the cell with the formula as a date. It will return the first Friday on or after the start date.
    If you have Excel 2010 or later, you can also use
    =A1+7-WEEKDAY(A1,16)
    Either formula can be filled down.
    Regards, Hans Vogelaar (http://www.eileenslounge.com)

  • 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 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

  • 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.

  • 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.

  • Query on fetching the no.of days between Invoice date and due date in rtf template embedded BI Publisher Report

    Hi Experts,
    We have a requirement to fetch the value of 'No of days' between Invoice date and due date (Two variable date fields on the template) in an Analysis.
    Please let me know the procedure of how to achieve the same.
    Regards,
    Rev

    it's good for ideas but implementation a bit different
    Oracle Business Intelligence Publisher Report Designer's Guide
    This function provides a method to get the difference between two dates in the given locale. The dates need to be in "yyyy-MM-dd" format. This function supports only the Gregorian calendar. The syntax is as follows:
    <?xdoxslt:date_diff(‘format’, ‘YYYY-MM-DD’, ‘YYYY-MM-DD’, $_XDOLOCALE, $_XDOTIMEZONE)?>
    where
    format is the time value for which the difference is to be calculated
    Example:
    <?xdoxslt:date_diff(‘d’, ‘2006-04-08’, ‘2006-04-01’, $_XDOLOCALE, ‘America/Los_Angeles’)?>
    returns
    -7

Maybe you are looking for