Oracle PL/SQL 上年和上月

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16240542/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:37:28  来源:igfitidea点击:

Oracle PL/SQL previous year and month

sqloracleplsql

提问by Pinu

I need to write a script in pl/sql where I get the previous period. Period in the accounting system is defined as YYYYMM i.e for this month the current period would be 201304 and previous months period would be 201303.

我需要在 pl/sql 中编写一个脚本来获取上一期。会计系统中的期间定义为 YYYYMM,即本月当前期间为 201304,上个月期间为 201303。

For one of my functions a period is passed I need to get the previous period. So for example if the period that is passed is 201301 then the query needs to return 201212

对于我的一个功能,过了一段时间我需要获取上一期。例如,如果传递的时间段是 201301 那么查询需要返回 201212

回答by Todd Gibson

SQL only:

仅 SQL:

SELECT TO_CHAR(ADD_MONTHS(TO_DATE(current_period, 'YYYYMM'), -1), 'YYYYMM') 
FROM dual;

PL/SQL:

PL/SQL:

previous_period := TO_CHAR(ADD_MONTHS(TO_DATE(current_period, 'YYYYMM'), -1), 'YYYYMM');