oracle 如何从给定日期获取一个月的最后一天?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15445216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get last day of a month from a given date?
提问by Hoan Dang
For example, the given date is 04/04/1924and I want to find out the last day of February of the year 1924.
例如,给定的日期是04/04/1924,我想找出 1924 年 2 月的最后一天。
I came up with the add_month but it seems not flexible if I have different given month from data source
我想出了 add_month 但如果我的给定月份与数据源不同,它似乎不灵活
Any good ideas ?
有什么好主意吗?
回答by Mitch Wheat
Oracle has a last_day()
function:
甲骨文有一个last_day()
功能:
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()
.
Add_Months()
在您的日期使用以获得适当的月份,然后申请last_day()
。
回答by manjiri
query inpl sql to get first day and last day of the month :
查询 inpl sql 以获取该月的第一天和最后一天:
first day :
第一天 :
select to_date(to_char(LAST_DAY(sysdate),'YYYYMM'),'YYYYMM') from dual;
Last day:
最后一天:
select LAST_DAY(to_date(to_char((sysdate),'YYYYMM'),'YYYYMM')) from dual;
回答by Abid Mehdi
Get First and Last Date of the Month Just change the Month digit to get the first and last date of the month
获取本月的第一个和最后一个日期只需更改月份数字即可获取该月的第一个和最后一个日期
select
to_date('01/'|| '07/' || to_char(sysdate, 'YYYY'), 'dd/mm/yyyy') first,
last_day(to_date('01/'|| '07/' || to_char(sysdate, 'YYYY'), 'dd/mm/yyyy')) last
from dual
Result:
结果:
first last
--------------------------
01/02/2017 28/02/2017
回答by bilal Tashfeen
the simple way to do is :
简单的方法是:
select first_name , last_day(hire_date) from employees;
i am using HR schema... you can get the last day of the mentioned month.
我正在使用 HR 模式...您可以获得所提到月份的最后一天。
回答by Civa
This will show you the last day of month
这将显示每月的最后一天
SELECT Max(LAST_DAY(ADD_MONTHS(SYSDATE, -2)) + level)
FROM dual
CONNECT BY
level <= LAST_DAY(ADD_MONTHS(SYSDATE, -1)) - LAST_DAY(ADD_MONTHS(SYSDATE, -2))
you can replace system date what ever date you want with to_date('04/04/1924','MM-DD-YYYY')
你可以用 to_date('04/04/1924','MM-DD-YYYY') 替换你想要的任何日期的系统日期
SELECT Max(LAST_DAY(ADD_MONTHS(to_date('04/04/1924','MM-DD-YYYY'), -2)) + level)
FROM dual
CONNECT BY
level <= LAST_DAY(ADD_MONTHS(to_date('04/04/1924','MM-DD-YYYY'), -1)) - LAST_DAY(ADD_MONTHS(to_date('04/04/1924','MM-DD-YYYY'), -2))
or simply
或者干脆
select LAST_DAY(ADD_MONTHS(to_date('04/04/1924','MM-DD-YYYY'), -2)) from dual;
回答by Adel
to get the selected month and year last day:
获取最后一天选定的月份和年份:
SELECT TO_CHAR(LAST_DAY(TO_DATE(:X_THE_MONTH ||'/01/' || :X_THE_YEAR,'MM/DD/YYYY')), 'dd') FROM DUAL
SELECT TO_CHAR(LAST_DAY(TO_DATE(:X_THE_MONTH ||'/01/' || :X_THE_YEAR,'MM/DD/YYYY')), 'dd') FROM DUAL
回答by Jorgechu
if you want know if your date is final mount
如果你想知道你的约会是否是最后一次
SELECT
case when
TO_DATE('19240430','YYYYMMDD') = LAST_DAY(TO_DATE('04/04/1924','MM/DD/YYYY'))
THEN 1 ELSE 0 END LAST_MOUNTH_DAY FROM DUAL