如何从给定日期获得一个月的最后一天?
问题描述:
例如,给定的日期是 04/04/1924 ,我想找出1924年2月的最后一天.
For example, the given date is 04/04/1924 and I want to find out the last day of February of the year 1924.
我想出了add_month,但是如果我给定的月份与数据源不同的话,这似乎并不灵活
I came up with the add_month but it seems not flexible if I have different given month from data source
有什么好主意吗?
答
Oracle具有 last_day()
函数:
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;
结果:
April, 30 1924 00:00:00+0000
March, 31 1924 00:00:00+0000
February, 29 1924 00:00:00+0000
在您的约会日期使用 Add_Months()
获取适当的月份,然后应用 last_day()
Use Add_Months()
on your date to get the appropriate month, and then apply last_day()
.