The end of the month selection

Hi all

We have a table say xyz with two columns month_cd and end_date where month is a code for each month and the end of the month is an end date of the regular calendar months and months where the end of the month is a day of weekend so for these months End_date is entered by a user in this table by another application. and we will have this information for al months in privious and current of the year

for ex:

Month_cd end_date
201101 01/30/2011
201102 03/04/2011
201103 03/31/2011
201104 04/30/2011
201105 06/04/2011

Now I have pick months based on the end_date, I mean everytime I run a query to select month_cd it must show date of cd for the month up to the endof month (maybe not a calendar end date)

How can achieve us? Please help me.

SQL and PL - SQL forum would be more appropriate for this type of question, and you should always mention the full version of Oracle you are using (for example, 10.2.0.4).

Depending on the version of Oracle, there are several possible solutions. Here are a few that you can try:

--sample data:
with t as (
select 201101 as month_cd, to_date('01/30/2011','mm/dd/yyyy') as end_date from dual union all
select 201102 as month_cd, to_date('03/04/2011','mm/dd/yyyy') as end_date from dual union all
select 201103 as month_cd, to_date('03/31/2011','mm/dd/yyyy') as end_date from dual union all
select 201104 as month_cd, to_date('04/30/2011','mm/dd/yyyy') as end_date from dual union all
select 201105 as month_cd, to_date('06/04/2011','mm/dd/yyyy') as end_date from dual
)
--end of sample data
select max(month_cd) keep (dense_rank first order by case when end_date >= trunc(sysdate) then end_date end nulls last) as month_cd
from t;
--sample data:
with t as (
select 201101 as month_cd, to_date('01/30/2011','mm/dd/yyyy') as end_date from dual union all
select 201102 as month_cd, to_date('03/04/2011','mm/dd/yyyy') as end_date from dual union all
select 201103 as month_cd, to_date('03/31/2011','mm/dd/yyyy') as end_date from dual union all
select 201104 as month_cd, to_date('04/30/2011','mm/dd/yyyy') as end_date from dual union all
select 201105 as month_cd, to_date('06/04/2011','mm/dd/yyyy') as end_date from dual
)
--end of sample data
select month_cd from (
select month_cd, end_date, (lag(end_date,1,to_date('01/01/1900','mm/dd/yyyy')) over(order by end_date))+1 as begin_date
from t
)
where trunc(sysdate) between begin_date and end_date
;

or

--sample data:
with t as (
select 201101 as month_cd, to_date('01/30/2011','mm/dd/yyyy') as end_date from dual union all
select 201102 as month_cd, to_date('03/04/2011','mm/dd/yyyy') as end_date from dual union all
select 201103 as month_cd, to_date('03/31/2011','mm/dd/yyyy') as end_date from dual union all
select 201104 as month_cd, to_date('04/30/2011','mm/dd/yyyy') as end_date from dual union all
select 201105 as month_cd, to_date('06/04/2011','mm/dd/yyyy') as end_date from dual
)
--end of sample data
select month_cd
from t
where end_date=(
  select min(end_date)
  from t where end_date >= trunc(sysdate)
  )
;

Kind regards
Bob

Tags: Database

Similar Questions

  • How to generate 2,500 employees calendar for the month selected by Pl/sql

    Gusy

    I have an employee table which is contain about 2500 employees and every month we will have to generate a list of each employee, let assume that each employee will be last_day insert the records of the month in the calendar table.
    , as January day total 31, it's average system will insert record 2500 * 31.
    How can I generate table & what is the fastest way.

    Please, need help to expand.


    Thank you.

    Hello

    Hope that the below given SQL help, you get the solution

    SELECT LAST_DAY(TO_DATE('01-'||LEVEL||'-2010','DD-MM-YYYY')) - TO_DATE('01-'||LEVEL||'-2010','DD-MM-YYYY') + 1 DAYS_IN_MONTH
    FROM DUAL
    CONNECT BY LEVEL <= 12;
    

    * 009 *.

  • TIME REMAINING IN SECONDS FROM THE END OF THE MONTH

    Dear all,

    How to calculate the time remaining is seconds to the end of the month?

    time difference in seconds of sysdate to the last day of the month till 23:59:59

    Thanks in advance

    Hello

    SELECT (ADD_MONTHS (TRUNC (SYSDATE, 'MONTH')

    1

    )

    -SYSDATE

    ) * 24 * 60 * 60 AS seconds_left

    DOUBLE;

    SYSDATE is the date and time.

    TRUNC (SYSDATE, 'MONTH') is the beginning of the current month

    ADD_MONTHS (TRUNC (SYSDATE, 'MONTH'), 1) is the beginning of the next month

    ADD_MONTHS (TRUNC (SYSDATE, 'MONTH'), 1)-SYSDATE is the number of days (not necessarily an integer) by the end of the month

    (ADD_MONTHS (TRUNC (SYSDATE, 'MONTH'), 1)-SYSDATE) * 24 is the number of hours (not necessarily an integer) by the end of the month

    ...

  • Breaking a year in coming weeks of beginning and end Dates (parameters) and first and last day of the month

    Hello people:

    I have currently a query that picks up all the weeks between a date range where my week starts on Thursday and ends on Wednesday. The following query works fine except that I need the week of beginning and end, from beginning and end Dates of months and the beginning and the Dates of end of the values of the settings, I'm passing. In this case, I chose January 1, 2013 to 31 December 2013.

    Any help or pointers would be great!

    Thank you!

    Problem: I am picking up days of December 2012 as the first week began on December 27, 2012. In addition, in December, I'm pretty much lost last week as well (26 December - 31 December).

    The request in hand:

    SELECT first_thursday + (7 * (LEVEL - 1))   AS week_start_date,
                     first_thursday + (7 * LEVEL) - 1    AS  week_end_date
    FROM
    (
      SELECT TRUNC(p_from_date + 4, 'IW') - 4   AS first_thursday, -- Week should start the pre ceeding THURSDAY based on the Start Date
                      TRUNC( p_to_date + 4,  'IW') - 5     AS last_wednesday
      FROM
      (
        SELECT to_date('01-JAN-2013') AS p_from_date,
                        NVL(to_date('31-DEC-2013'), SYSDATE) AS p_to_date
        FROM     dual
      ) parms
    ) end_points
    CONNECT BY LEVEL <= ( last_wednesday + 1 - first_thursday)/7;
    

    Currently, this is the result I get (I'm only including the months of January and December here).

    Week_Start_Date     Week_End_Date
    27-DEC-12                02-JAN-13
    03-JAN-13                 09-JAN-13
    10-JAN-13                 16-JAN-13
    17-JAN-13                 23-JAN-13
    24-JAN-13                 30-JAN-13
    31-JAN-13                 06-FEB-13
    

    December:

    28-NOV-13               04-DEC-13
    05-DEC-13              11-DEC-13
    12-DEC-13              18-DEC-13
    19-DEC-13              25-DEC-13
    

    What I would really like is based on start and end Dates, as well as the first and the last day of the month is similarly try nicely. I have provided and then gently break at the end of the month:

    January:

    01-JAN-13              02-JAN-13
    03-JAN-13              09-JAN-13
    10-JAN-13             16-JAN-13
    17-JAN-13             23-JAN-13
    24-JAN-13             30-JAN-13
    31-JAN-13             31-JAN-13
    

    February:

    01-FEB-13    06-FEB-13
    07-FEB-13    13-FEB-13
    14-FEB-13    20-FEB-13
    21-FEB-13    27-FEB-13
    28-FEB-13    28-FEB-13
    

    November:

    31-OCT-13    06-NOV-13
    07-NOV-13    13-NOV-13
    14-NOV-13    20-NOV-13
    21-NOV-13    27-NOV-13
    28-NOV-13    04-DEC-13
    

    December:

    01-DEC-13           04-DEC-13
    05-DEC-13           11-DEC-13
    12-DEC-13          18-DEC-13
    19-DEC-13          25-DEC-13
    26-DEC-13          31-DEC-13
    

    Hello

    Roxyrollers wrote:

    Hello people:

    I have currently a query that picks up all the weeks between a date range where my week starts on Thursday and ends on Wednesday. The following query works fine except that I need the week of beginning and end, from beginning and end Dates of months and the beginning and the Dates of end of the values of the settings, I'm passing. In this case, I chose January 1, 2013 to 31 December 2013.

    Any help or pointers would be great!

    Thank you!

    Problem: I am picking up days of December 2012 as the first week began on December 27, 2012. In addition, in December, I'm pretty much lost last week as well (26 December - 31 December).

    The request in hand:

    1. First_thursday SELECT + (7 * (LEVEL - 1)) AS week_start_date,
    2. first_thursday + (7 * LEVEL)-1 AS week_end_date
    3. Of
    4. (
    5. SELECT TRUNC (p_from_date + 4, 'IW') - 4 AS first_thursday,-week should start to perform the pre-mounted THURSDAY based on the Start Date
    6. TRUNC (p_to_date + 4, 'IW') - 5 AS last_wednesday
    7. Of
    8. (
    9. SELECT to_date('01-JAN-2013') AS p_from_date,
    10. NVL (to_date('31-Dec-2013'), SYSDATE) AS p_to_date
    11. OF the double
    12. ) parms
    13. ) end_points
    14. CONNECT BY LEVEL<= (="" last_wednesday="" +="" 1="" -="">

    Currently, this is the result I get (I'm only including the months of January and December here).

    1. Week_Start_Date Week_End_Date
    2. DECEMBER 27, 12 2 JANUARY 13
    3. JANUARY 3, 13 JANUARY 9, 13
    4. 10 JANUARY 13 JANUARY 16, 13
    5. 17 JANUARY 13 23 JANUARY 13
    6. 24 JANUARY 13 30 JANUARY 13
    7. 31 JANUARY 13 FEBRUARY 6, 13

    December:

    1. NOVEMBER 28, 13 4 DECEMBER 13
    2. 5 DECEMBER 13 DECEMBER 11, 13
    3. 12 DECEMBER 13 18 DECEMBER 13
    4. 19 DECEMBER 13 DECEMBER 25, 13

    What I would really like is based on start and end Dates, as well as the first and the last day of the month is similarly try nicely. I have provided and then gently break at the end of the month:

    January:

    1. JANUARY 1, 13 2 JANUARY 13
    2. JANUARY 3, 13 JANUARY 9, 13
    3. 10 JANUARY 13 JANUARY 16, 13
    4. 17 JANUARY 13 23 JANUARY 13
    5. 24 JANUARY 13 30 JANUARY 13
    6. 31 JANUARY 13 JANUARY 31, 13

    February:

    1. 1ST FEBRUARY 13 FEBRUARY 6, 13
    2. 7 FEBRUARY 13 FEBRUARY 13, 13
    3. 14 FEBRUARY 13 FEBRUARY 20, 13
    4. 21 FEBRUARY 13 FEBRUARY 27, 13
    5. 28 FEBRUARY 13 FEBRUARY 28, 13

    November:

    1. 31 OCTOBER 13 NOVEMBER 6, 13
    2. 7 NOVEMBER 13 NOVEMBER 13, 13
    3. 14 NOVEMBER 13 NOVEMBER 20, 13
    4. 21 NOVEMBER 13 NOVEMBER 27, 13
    5. NOVEMBER 28, 13 4 DECEMBER 13

    December:

    1. 1ST DECEMBER 13 DECEMBER 4, 13
    2. 5 DECEMBER 13 DECEMBER 11, 13
    3. 12 DECEMBER 13 18 DECEMBER 13
    4. 19 DECEMBER 13 DECEMBER 25, 13
    5. 26 DECEMBER 13 DECEMBER 31, 13

    Why the "weeks" in November, have all 7 days, while the "weeks" in the first or the last week in the other month usually have less?  (For example, February 28 is a 'week' alone.)

    The following works for the other months:

    WITH all_days AS

    (

    SELECT DATE "2013-01-01' + LEVEL - AS a_date 1

    OF the double

    CONNECT BY LEVEL<=>

    )

    SELECT DISTINCT

    More GRAND (TRUNC (a_date + 4, 'IW') - 4)

    , TRUNC (a_date, 'MONTH')

    ) AS week_begin

    , The LEAST (TRUNC (a_date + 4, 'IW') + 2

    TRUNC (LAST_DAY (a_date))

    ), Week_end

    Of all_days

    ORDER BY week_begin

    ;

    Output:

    WEEK_BEGIN WEEK_END

    ----------- -----------

    January 1, 2013 January 2, 2013

    January 3, 2013 January 9, 2013

    January 10, 2013 January 16, 2013

    January 17, 2013 January 23, 2013

    January 24, 2013 January 30, 2013

    January 31, 2013 January 31, 2013

    February 1, 2013 February 6, 2013

    February 7, 2013 February 13, 2013

    February 14, 2013 February 20, 2013

    February 21, 2013 February 27, 2013

    February 28, 2013 February 28, 2013

    ...

    November 1, 2013 November 6, 2013

    November 7, 2013 November 13, 2013

    November 14, 2013 November 20, 2013

    November 21, 2013 November 27, 2013

    November 28, 2013 November 30, 2013

    December 1, 2013 December 4, 2013

    December 5, 2013 December 11, 2013

    December 12, 2013 December 18, 2013

    December 19, 2013 December 25, 2013

    26 December 2013 31 December 2013

  • Using the interval function works well for the end of the month


    I need to be able to get 1 year, 2 years, etc and also 1 month 2 months ago for each date in the calendar.  I can use the function of the INTERVAL for the most part, but it does not work if there is 1 month (year) would produce an invalid date.

    For example, the monitoring generates an error.

    Select TO_DATE (February 29, 2012 ')- INTERVAL of '1' YEAR  FROM DUAL; 

    Select TO_DATE('31-MAR-2011') - MONTH INTERVAL '1' FROM DUAL;

    Is there a way to get chosen to return on February 28, 2011?

    Thank you for your help.

    Sandy

    Hi, Sandy,

    ADD_MONTHs always returns a valid DATE.

    ADD_MONTHS (TO_DATE ((29 février 2012 ', «DD-MON-YYYY»), 12 * n))

    will return on February 29, n years after 2012, if this year is a leap year and February 28 this year, if it is not a leap year.

    In general

    ADD_MONTHS (d, m)

    Returns the date months after DATE m d.  If d is located near the end of his month and months m months in the future is for several days, then ADD_MONTHS returns the last day of that month.  By example, if d is on 31 March and 1 m, then ADD_MONTHS returns the last day in April (1 month after March), because there is no day 31 in April.

  • Select records based on the monthly anniversary date

    Hello

    I have a table with a field of date_added and I want to select records from monthly birthday of this field.

    for example. ID, Date_added
    1, DECEMBER 10, 2012
    2, 11 NOVEMBER 2012
    3, 10 MARCH 2012
    4, FEBRUARY 28, 2012
    5, 30 DECEMBER 2012

    So for January 10, 2013, I would like to return the documents 1 and 3 only

    I started watching the extract function, but this little falls down to the records at the end of the month. For example, on 28 February, I also include records where 29, 30 or 31 is the day of date_added. So, in the above table, I won't return documents 4 and 5, but retrieves only returns 4.

    Is there a simple function to make the request of birthday this month - have I missed something very obvious? Or I have to write a query to explicitly dates at the end of the month? So far I have not found a reasonable solution!

    I use 11g

    Thank you

    user11259857 wrote:
    Hi Al,.

    No I don't think that there is a flaw. For example, the 30 Jan is not the last day of the month, but is June 30. Thus, on 30 January I want only to interrogate all the dates falling on the 30th - 28 February, after already being interrogated on the 28 Jan. However, on 30 June, I would query the 30th of each month, as well as the 31 January, March, may, July, Aug, Oct, dec. February is a special case, being both shorter and a leap year

    Thank you

    Richard

    Hi Richard,

    so, we can assume that your needs are:

    If the day is the last day of that month, interview the day of the other months being > = day of the current month.

    That is to say:

    Jan-30 = query only 30 other months
    Jan-31 = query only 31 of the other months

    Feb-28 (non-leap years) = question 28, 29, 30 and 31 other months
    29 February (leap year) = query 29,30,31 to other months

    Mar-30 = query only 30 other months
    Apr-30 = query 30 and 31 other months

    This hypothesis is correct?

    If so, then something like this should work:

    WITH mydates(id, date_added) AS
    (
       SELECT 1, TO_DATE('10-DEC-2012', 'DD-MON-YYYY') FROM DUAL UNION ALL
       SELECT 2, TO_DATE('11-NOV-2012', 'DD-MON-YYYY') FROM DUAL UNION ALL
       SELECT 3, TO_DATE('10-MAR-2012', 'DD-MON-YYYY') FROM DUAL UNION ALL
       SELECT 4, TO_DATE('28-FEB-2012', 'DD-MON-YYYY') FROM DUAL UNION ALL
       SELECT 5, TO_DATE('30-DEC-2012', 'DD-MON-YYYY') FROM DUAL
    )
    SELECT id, date_added
      FROM mydates
     WHERE (:input_date = LAST_DAY(:input_date) AND TO_CHAR(:input_date,'DD') <= TO_CHAR(date_added, 'DD'))
        OR TO_CHAR(:input_date,'DD') = TO_CHAR(date_added, 'DD');
    
    with input_date = 28/02/2013
    
    id   date_added
    4    28/02/2012
    5    30/12/2012
    

    Kind regards.
    Al

    Published by: Alberto Faenza on 10 January 2013 15:21
    Added example

  • Virgins of the days in the calendar at the end of a month

    Calendar in OSX on a regular Mac, every end of month continues seamlessly in the following month if the last week of the month end and the first week of the month start appear like a week continues

    In calendar, in iOS, like on an iPad, each month ends with empty days such as the last week of the month end ends on a single line with the first week of the month begins on the next line.

    As a result, that an event that fills the end appears to 'break' at the end of the month.  It is visually distracting and I can't find an option to set the schedule of the iOS, so it looks the same as OSX calendar

    Is this one?

    There is no option to change the which unfortunately. IOS users just need to deal with it or find a third-party calendar that is more to their liking. (There is a reason that I do not use a lot of calendar application)

  • As of Windows XP support ends in 2014, will I still be able to get anti virus updates and all the months of security beyond 2014 patches

    As of Windows XP support ends in 2014, will I still be able to get anti virus updates and all the months of security beyond 2014 for Windows XP patches?

    As of Windows XP support ends in 2014, will I still be able to get anti virus updates and all the months of security beyond 2014 for Windows XP patches?

    From April 2014, no security/more operating system patches released at Microsoft (for the most part) for Windows XP.  It's a BONE death in the eyes of its creator.
     
    Your anti-virus software may or may not be supported - which depends on its own end of life such as decided by its supplier, and even if it is a Microsoft product, is not directly related to the lifecycle of Windows XP (you can say that as Windows XP came with no native antivirus/antimalware features.)

  • I want to use InDesign, but for this month only! so if I pay for the "monthly plan" means that I pay for this month then the plan InDesign will end?

    I want to use InDesign, but for this month only! so if I pay for the "monthly plan" means that I pay for this month then the plan InDesign will end?

    Hello

    Please go through the Adobe - General conditions of subscription as well.

    Alternatively, you can contact customer service

    Hope that helps!

    Kind regards

    Sheena

  • subscription to the month for creative cloud photoshop ended due to no money in bank account, it will b auto renewed once the money is there once again in the Bank? How to cancel it permanently?

    subscription to the month for creative cloud photoshop ended due to no money in bank account, it will b auto renewed once the money is there once again in the Bank? How to cancel it permanently?

    Liliane,

    To cancel the Member please contact our support team via: http://helpx.adobe.com/in/contact.html?step=CCSN_adobe-id-signing-in_stillNeedHelp

    If you cannot use the chat nicely option, try the steps mentioned below.

    • Try a different browser.
    • Clear cookies and cache

    If you are still not able to discuss, good response with the below mentioned information.

    • OS version:
    • Browser version:

    You can also try: http://helpx.adobe.com/x-productkb/global/phone-support-orders.html

    For more information on the cancellation: https://helpx.adobe.com/x-productkb/policy-pricing/cancel-membership-subscription.html

    Atul_Saini

  • Determine the end of the month and then run the script

    Hi all

    OK, I'm stumped. I can get all kinds of data to date... but I did not find a way (or the same example script) that shows how to retrieve the end of the month.


    Why?


    I have a master script that runs as a scheduled task. It runs every day and call other scripts based on the date. It is great for things like weekly reports that I just used $dow = {get-date}. DayofWeek but I need to run scripts at the end of the month.

    I can something together that gets the day, and then performs a logical ugly pop-based < or > but I don't know there's a smoother way to do.

    As always, any help greatly appreciated.

    See you soon

    Hello, medalklector-

    One way to do it, it is described by RMToups, Jr. at http://activelydirect.blogspot.com/2011/01/determine-last-day-of-month-in.html.  You should be able to set the output to fit your needs.  How does do for you?

  • Select the query to select the month range based on the current date.

    It's the database 10 g oracel.

    I want to choose two months of data in the table. But these two months should be based on the current date.

    For example.

    If I run the select query on July 7, 2013, then the query must ignore the current month being July 2013 and last month, which is June 2013 and it shoud select only may 2013 and April 2013.

    Can someone help me how to make this request.

    SELECT *.

    FROM my_table

    WHERE my_date > = TRUNC (ADD_MONTHS (sysdate,-3), 'MY') - first day of the month 3 months ago

    AND my_date< trunc(add_months(sysdate,-1),'mon')="" --="" first="" day="" of="" last="">

  • Report view columns based on the selection of the month

    Hello

    I have an interactive report.
    In my report, I show the columns below. Let's say the show for months salary of the EMP for this month

    EmpID, ename, jan, Feb, mar, Apr, may, June, July, August, Sept, oct, nov, dec

    I have two selection lists (to filter the report) in my interface. P1_MONTH and P1_YEAR. These have static LOVs.
    Jan (Display) - 01 (Return)
    (Display) - 02 Feb (Return) and so on.

    I am facing problem while hiding under certain conditions the columns.
    For example, if the user selects the month "Mar." Then the report should display

    EmpID, ename, jan, Feb, mar

    Another example, if the month is "Jun" then

    EmpID, ename, jan, Feb, mar, Apr, may, June

    So he must hide under certain conditions the columns based on selection.
    I put the conditional 'view' in the column of the report for "expression of the ar-ticle/column value 1 = expression 2'.

    P1_MONTH (in the expression 1)
    + 06,07,08,09,10,11,12 (in the expression for the month of June selection 2) +.

    But doesn't the show/hide.
    Am I missing something? Please correct me if I'm wrong.

    I use APEX 4.1 & g 10 DB

    Thank you in advance,
    Daniel

    This

    value of item/column expression 1 = expression 2
    

    is equivalent to a PL/SQL expression, such as

    :P1_MONTH = '06,07,08,09,10,11,12'
    

    So maybe, select a PL/SQL expression condition and write something like that for the month of March - if I interpret correctly the your needs.

    TO_NUMBER(:P1_MONTH) <= 3
    

    Scott

  • Select the first 20 days of the month for a given date

    Hi gurus,

    Please let me know to select the first 20 dates for a date supplied by using a parameter.

    For ex -.

    Suppose I provided a date by using a parameter - January 13, 2012

    The query should return me - 01-Jan-2012, January 2, 2012... January 20, 2012

    or if I get home on 23 March 2012

    The query should return me - 01-Mar-2012, March 2, 2012... 20 March 2012

    Thanks in advance.

    Hello

    Here's one way:

    VARIABLE  target_date     VARCHAR2 (11)
    EXEC     :target_date := '13-Jan-2012';
    
    SELECT      TO_DATE ( SUBSTR (:target_date, 4)
                , 'Mon-YYYY'
               ) + LEVEL - 1     AS a_date
    FROM     dual
    CONNECT BY     LEVEL     <= 20
    ;
    

    Note that this does not take account the part of: target_date that represents the day of the month. When you use TO_DATE without giving a date of the month, it defaults to the 1st of the month.

  • Select SQL - last day of the month, a year from now

    Hello
    How do we get the last day of the month, a year from now.

    For example-sysdate-12/2/2009
    Result - 31/12/2009

    Thank you

    Hello

    SELECT  LAST_DAY ( ADD_MONTHS ( SYSDATE
                                  , 12
                )           )
    FROM    dual;
    

    Remember, all DATEs, including the results of this query, include the hours minutes and seconds.
    The hours, the minutes and the seconds returned by the above expression are the same as SYSDATE: If you run it at 15:43:30 today, it returns 15:43:30 on December 31, 2010. This can be important if you want to use an expression like this as the cut-off point in a WHERE clause.

Maybe you are looking for