How to get last day of a month from a given date?

前端 未结 7 1385
长情又很酷
长情又很酷 2021-02-12 14:18

For example, the given date is 04/04/1924 and I want to find out the last day of February of the year 1924.

I came up with the add_month but it

7条回答
  •  盖世英雄少女心
    2021-02-12 15:00

    Oracle has a last_day() function:

    SELECT LAST_DAY(to_date('04/04/1924','MM/DD/YYYY')) from dual;
    
    SELECT LAST_DAY(ADD_MONTHS(to_date('04/04/1924','MM/DD/YYYY'), -1)) from dual;
    
    SELECT LAST_DAY(ADD_MONTHS(to_date('04/04/1924','MM/DD/YYYY'), -2)) from dual;
    

    Results:

    April, 30 1924 00:00:00+0000
    
    March, 31 1924 00:00:00+0000
    
    February, 29 1924 00:00:00+0000
    

    Use Add_Months() on your date to get the appropriate month, and then apply last_day().

提交回复
热议问题