Get the first and the last day of the month...

Hi friends,

I'm trying to get the first and last day of a month and wants to implement that in the following code:

SELECT COALESCE (Date_A, Date_B, Date_C)
OF the double

Here Date A and B are in Format DD MM YYYY (March 14, 2008)
and Date like MM YYYY (March 2008)

How can I get the Date_C as of March 1, 2008, OR March 31, 2008 if Date A and B are NULL?

Thank you!

Published by: user11095386 on April 23, 2009 10:45

Hello

In my previous message, I thought that you were starting with strings like ' 03 12 2009"and you want to display in the form of" 12 March 2009'.» If what you have is just the opposite, then simply reverse strings format in my first message. Add a comma, if you want, in the format string.
I think that's what you want:

COALESCE ( TO_CHAR ( TO_DATE ( Date_A, 'fmMonth DD YYYY'), 'MM DD YYYY')
         , TO_CHAR ( TO_DATE ( Date_B, 'fmMonth DD YYYY'), 'MM DD YYYY')
         , TO_CHAR ( TO_DATE ( Date_C, 'fmMonth YYYY'),    'MM DD YYYY')
         )

Notice how, on the 3rd line, TO_DATE is called without DD in the format string:

TO_DATE ( Date_C, 'fmMonth YYYY')

When you do so, by default the day the 1st of the month, so that's all you need to do to convert the VARCHAR2 'March 2009' at the DATE of March 1, 2009.

If you would like the last day of the month, not the first, when Date_C is selected, then use LAST_DAY:

COALESCE ( TO_CHAR ( TO_DATE ( Date_A, 'fmMonth DD YYYY'), 'MM DD YYYY')
         , TO_CHAR ( TO_DATE ( Date_B, 'fmMonth DD YYYY'), 'MM DD YYYY')
         , TO_CHAR ( LAST_DAY ( TO_DATE ( Date_C
                                          , 'fmMonth YYYY'
                            )
                     )
             , 'MM DD YYYY'
             )
         )

Tags: Database

Similar Questions

  • use to insert the last day of each month

    Hi, I wrote this code but it did not insert the 20 December and it is January 1, and next time running is 31 January... I would like to work to make this insert every last day of each month...
    begin
      sys.dbms_job.submit(job => :job,
                          what => 'INSERT INTO hisrtu 
                                            SELECT rtunum, linked, dbchkd, chaqty, dbvald, ctrltr, meaadj, qtyrtu, month, year, entity, rtunam 
                                            FROM qtyrtu;',
                          next_date => to_date('31-10-2010 01:00:03', 'dd-mm-yyyy hh24:mi:ss'),
                          interval => 'SYSDATE +30');
      commit;
    end;
    /
    I don't know if it's because I assigned the date 31-10... In November it's just but December has not...

    Best regards, hope you can help me.

    I use oracle 9i

    Hello

    If you want the job to run at 02:00 the last day of the next month, then the desired argument is:

    interval => 'ADD_MONTHS (TRUNC (SYSDATE, ''MONTH''), 2) - (22 / 24)'
    

    In other words, if she finishes running at 02:30 on 31 January, then
    TRUNC (SYSDATE, 'MONTH') will be at 00:00:00 January 1
    ADD_MONTHS (TRUNC (SYSDATE, 'MONTH'), 1) will be March 1st at 00:00:00
    ADD_MONTHS (TRUNC (SYSDATE, 'MONTH'), - 1) (22 / 24) will be 22 hours earlier, i.e. 02:00 on the last day of February.

    logandro wrote:
    I don't know if it's because I assigned the date 31-10... In November it's just but December has not...

    Do you mean this line?

    next_date-online to_date (October 31, 2010 01:00:03 ',' dd-mm-yyyy hh24:mi:ss'),

    I don't think that's a problem. The next_date specifies only when it work baptisms. I guess he ran on October 31; you said it was running well on 30 November, 30 days after that. Something has to happen after November 30.

    Note that the interval argument is relative when the task completes. If a task takes 30 minutes to run, then

    interval => 'SYSDATE +30'
    

    means that, if it turns to
    01:00:03 October 31, then it will run again about
    01:30:03 November 30, around
    02:00:03 on December 30.
    02:30:03 January 29 and so on. If your business does that for a few seconds, it won't matter much.

    If you decide to use LAST_DAY, don't forget not that the hours-minutes-seconds referring LAST_DAY are the same as those of his argument.

  • get the month and year of date type

    Hi all
    I need to get the month and year of type date.

    For example
    select to_date('2011-01-17', 'yyyy-mm-dd') from dual;
    Required result:
    01-2011
    Any ideas?

    Thanks in advance,
    Bahchevanov.
    select to_char(to_date('2011-01-17', 'yyyy-mm-dd'),'mm-yyyy') from dual;
    

    HTH

  • First day/last day of each month

    Hello
    y at - it sql/script to get the first day/last day of each month.new to bi do not know the syntax

    If a point on the link to get these things will be highly appreciated

    Hello
    I suggest that you go through the following link

    (1) http://oracle.ittoolbox.com/groups/technical-functional/oracle-bi-l/obiee-date-comparison-1726677#M1729534

    Toget first/last day of the month

    (2) http://www.obinotes.com/2010/02/tip-to-get-firstday-lastday.html

    New to OBIEE---> http://obiee101.blogspot.com/2009/07/obiee-how-to-get-started.html

    Thank you
    Saichand.V

  • Is there one way other than to_char to get the month of the date field

    Is there one way other than to_char to get the month of the date field

    Hello

    raj4tech wrote:

    Is there one way other than to_char to get the month of the date field

    EXTRACT is one:

    SELECT INTERESTED (SYSDATE MONTHS) AS curr_month

    DOUBLE;

  • How to get the last day of a month for every 2 months for a given period?

    Hello

    Can is it some please let me know how to get the last day, last day of the week, the weekend last day, last Monday, one month for every 2 months for a given period?

    Thanks in advance.

    Try the below

    SELECT LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2))) lastday.

    BOX WHEN TO_CHAR (LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2))), 'DY') = 'SAT '.

    SO LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2)))-1

    WHERE TO_CHAR (LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2))), 'DY') = 'Sun '.

    THEN LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2)))-2

    Of OTHER LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2)))

    END as lastweekday,

    BOX WHEN TO_CHAR (LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2))), 'DY') IN ('Sam', 'SUN')

    THEN LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2)))

    Of OTHER LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2)))

    -(TO_NUMBER (TO_CHAR (LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2))),' from)) - 1).

    END as lastweekendday,

    BOX WHEN TO_CHAR (LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2))), 'DY') = 'MY

    THEN LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2)))

    Of OTHER NEXT_DAY (LAST_DAY (ADD_MONTHS (TRUNC(startdate,'MM'), ((LEVEL*2)-2))), "LUN")-7

    END AS lastmonday

    FROM (SELECT SYSDATE startdate,

    SYSDATE + 300 enddate

    THE DOUBLE)

    CONNECT BY LEVEL<=>

  • to get the month and year separated

    Hello

    I have the entry_date_fin of column (type of data-varchar2 (8))

    with values like Apr.-2008
    Feb-2009 etc.

    can I get the distinct month and year separated
    i tried select to_date(entry_date_fin,'MM') FROM  xyz  doesnt wrk
    kindly guide me

    Although you are (wrongly, IMO) storage of dates as strings, just use string manipulation...

    SQL> with t as (select 'FEB-2008' as txt from dual)
      2  --
      3  select substr(txt,1,3) as mnth
      4        ,substr(txt,5) as yr
      5  from t
      6  /
    
    MNT YR
    --- ----
    FEB 2008
    
    SQL>
    
  • How to get the month previous year?

    Hello

    I have to get the previous year three-months of the effective date, but I have a problem since the beginning of the year and it's because I set the incorrect query.

    I mean:

    I wrote this query

    SELECT TO_CHAR(SYSDATE,'YYYYMM')-1, 2-TO_CHAR(SYSDATE,'YYYYMM'), TO_CHAR(SYSDATE,'YYYYMM')-3

    FROM DUAL;

    in order to get (on mars):

    201502, 201501, 201412

    but unfortunately, I get:

    201502, 201501, 201500

    Can you help me?

    Thanks in advance!

    Hello

    Here's one way:

    SELECT TO_CHAR (ADD_MONTHS (SYSDATE-1), "YYYYMM") AS the m1

    , TO_CHAR (ADD_MONTHS (SYSDATE-2), "YYYYMM") AS m2

    , TO_CHAR (ADD_MONTHS (SYSDATE,-3), "YYYYMM") M3

    DOUBLE;

  • Restart or read courses at first and not last page

    We have a course with a questionnaire which must be passed.

    If the user does not pass must relaunch the course in order to randomize the quiz questions. We noticed that the course is the reopening last police page.

    \is there a way to force a project to open ONLY on the first page anywhere - off left? I realize that this would be unnecessary if the user also left having to start

    I think this is a job for a table of contents page, but can't think how

    help appreciated

    It is a SCORM file to load on an LMS, you must check the 'Never resume data' option under the Advanced button in the reports dialog box:

    If it is not transferred to an LMS, and you use the table of contents and biscuits or local storage for bookmarking, you will need to uncheck learning self-paced option in the parameters of the table of contents. But this option is cancelled by the LMS functionality if you publish a LMS.

  • How to get back first and Photoshop?

    Recently, I wiped my computer I had installed with Photoshop and first... How to make a comeback? I don't think that I've linked to my account online, so I can't get it through that.

    Just a long shot - try cat:

    Contact the customer service

  • I took an ICM4 course, I should get VCP4 first and then VCP5?

    Hi all

    I took ICM4 but do not yet have any VMware Certification and I am looking for it. Here are my questions:

    Should I get first VCP4 and then VCP5 or just forcus on VCP5?

    If I want to VCP5 without taken another course of ICM5, do I need to take the VCP4 exam as VCP5 before the end of February 2012 or I only need to get VCP4 before that date and I can take VCP5 review at any time thereafter?

    If I decide to go straight to the VCP5, shoud I'm just "what's new in vSphere 5 ' or 'ICM5' couse?

    Thanks in advance!

    All the certification paths are on the VMware website:

    http://myLearn.VMware.com/mgrReg/plan.cfm?plan=12457&UI=www

    IMHO: you can go to the VCP4 and before the end of February take also the consideration of the VCP5.

  • GET THE MONTH

    Hello

    I need to generate months between two bays. Last span should be up to sysmonth. I appreciate your time.

    With the help of 10g or 11g
    EFFECTIVE_MONTH     DIST_ID     SHOP_ID     SHOP_NAME
    01/01/2008     P00112     PFR00809     TOKEN GLASSES
    06/01/2009     P00112     PFR00536     MAVALI GLASSES
    Required result
    EFFECTIVE_MONTH     DIST_ID     SHOP_ID     SHOP_NAME     SHOP_MONTH
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Jan-2008
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Feb-2008
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Mar-2008
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Apr-2008
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-May-2008
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Jun-2008
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Jul-2008
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Aug-2008
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Sep-2008
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Oct-2008
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Nov-2008
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Dec-2008
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Jan-2009
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Feb-2009
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Mar-2009
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-Apr-2009
    1-Jan-2008     P00112     PFR00809     TOKEN GLASSES     1-May-2009
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Jun-2009
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Jul-2009
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Aug-2009
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Sep-2009
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Oct-2009
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Nov-2009
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Dec-2009
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Jan-2010
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Feb-2010
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Mar-2010
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Apr-2010
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-May-2010
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Jun-2010
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Jul-2010
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Aug-2010
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Sep-2010
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Oct-2010
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Nov-2010
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Dec-2010
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Jan-2011
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Feb-2011
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Mar-2011
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Apr-2011
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-May-2011
    1-Jun-2009     P00112     PFR00536     MAVALI GLASSES     1-Jun-2011

    Hello

    Here's one way:

    WITH     got_month_cnt     AS
    (
         SELECT     effective_month
         ,     dist_id
         ,     shop_id
         ,     shop_name
         ,     MONTHS_BETWEEN ( LEAD ( effective_month
                                  , 1
                              , ADD_MONTHS ( TRUNC (SYSDATE, 'MONTH')
                                           , 1
                                     )
                              ) OVER ( PARTITION BY  dist_id
                                    ORDER BY      effective_month
                                  )
                        , effective_month
                        )     AS month_cnt
         FROM    table_x
    )
    SELECT       c.effective_month
    ,       c.dist_id, c.shop_id, c.shop_name
    ,       ADD_MONTHS ( c.effective_month
                     , m.column_value - 1
                   ) AS shop_month
    FROM             got_month_cnt       c
    CROSS JOIN      TABLE ( CAST ( MULTISET ( SELECT  LEVEL
                                              FROM    dual
                                    CONNECT BY     LEVEL     <= c.month_cnt
                                ) AS sys.odcinumberlist
                     )      ) m
    ;
    

    I'm just guessing at what role (if any) dist_id, shop_id and shop_name play in this issue.

    I hope that answers your question.
    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) and also publish outcomes from these data.
    Explain, using specific examples, how you get these results from these data.
    Always tell what version of Oracle you are using.

    Published by: Frank Kulash, June 8, 2011 17:38

  • Get the number of days, months and year

    Hi all

    I'm using OBIEE 11 g. I have a line of dash for year (2010, 2011, etc.) and months (Jan, Feb,..., Dec). How can I get the number of days in a given month and year in my report?

    For example, if I choose to Jan & 2011, I'd get 31 and 356. And if I choose Feb & 2011, I would get 28 and 356.

    Thank you!

    I have something more good option here... Try to use this

    DAYOFMONTH (TIMESTAMPADD (SQL_TSI_DAY, DAYOFMONTH (CURRENT_DATE) *-1, TIMESTAMPADD (SQL_TSI_MONTH, 1, CURRENT_DATE)))

    This will give you the days for the month in progress (Oct 2011) after calculating the last day of each month... Similary dynamically pass the value Date or month... You will need to modify this function but shud be simple. Hope this helps :)

  • How to get the maximum number of days in a month?

    How to get the maximum number of days in a month?

    I use a dateField. where I can get the month. Now, I want to get the maximum number of days in a month.

    How can I get it?

    Please help me. and thanks in advance... []

    NET. RIM. Device.API.util
    Class DateTimeUtilities

    getNumberOfDaysInMonth (int month, int year)
    Returns the number of days in the specified month

    It took me like 10 seconds to find it in the API, I guess you spent more time writing the post...

  • request for return 2nd motnh first and last day

    Any body please give me the query back 2 months first day after the 2nd month last day?

    last month first day
    TIMESTAMPADD (SQL_TSI_MONTH-1, TIMESTAMPADD (SQL_TSI_DAY,-(DAY (CURRENT_DATE)-1), CURRENT_DATE))


    last month last day
    TIMESTAMPADD (SQL_TSI_DAY,-(DAY (CURRENT_DATE)), CURRENT_DATE)

    someone I care please months as above queries queries?
    2nd month back day -?

    2nd back months last day -?

    3rd month back day -?

    3rd month back last - day?

    Hello

    2nd month back day -?

    TIMESTAMPADD (SQL_TSI_MONTH,-2, TIMESTAMPADD (SQL_TSI_DAY,-(DAY (CURRENT_DATE)-1), CURRENT_DATE))

    2nd back months last day -?

    TIMESTAMPADD (SQL_TSI_MONTH-1, TIMESTAMPADD (SQL_TSI_DAY,-(DAY (CURRENT_DATE)), CURRENT_DATE))

    3rd month back day -?

    TIMESTAMPADD (SQL_TSI_MONTH,-3, TIMESTAMPADD (SQL_TSI_DAY,-(DAY (CURRENT_DATE)-1), CURRENT_DATE))

    3rd month back last - day?

    TIMESTAMPADD (SQL_TSI_MONTH,-2, TIMESTAMPADD (SQL_TSI_DAY,-(DAY (CURRENT_DATE)), CURRENT_DATE))

    Kind regards
    Srikanth

Maybe you are looking for

  • Wake on LAN - AirPort Extreme

    I'm trying to configure the Wake on LAN for my AirPort Extreme and Windows 7 PC, reboot with my iPad.  It works fine when the iPad is on the local network, but no power on the PC when I'm not on my network domestic.  I put airport to the port before

  • Pavilion: password administrator or power on password

    Hello My laptop asks a "password administrator or power on password" (I forgot my bios password). The code is 73433702. I looked on the Internet and found your site. Thanks in advance for your answer. Jeremy

  • HP Pavilion dv6-6c40us: change the processor of amd HP Pavilion dv6-6c40us PC notebook

    Hi, I need to know if I can change my laptop HP Pavilion dv6-6c40us Notebook PC processor roduct Specifications: Product name: dv6-6c40us US product number: A6Y50UA Microprocessor: 2.5GHz/1.6GHz VISION technology A8 from AMD with AMD Quad - Core A8 -

  • The ASR backup failure

  • CONNECTED HP - CANNOT CONNECT TO IT

    Yesterday, I had a problem in establishing an account because of an old Snapfish account, this has now been resolved... Thank you. Today I managed to get some things working, but I'm having a problem connecting to connected HP.  I click on the eprint