Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle PL/SQL previous year and month

Tags:

sql

oracle

plsql

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.

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

like image 247
Pinu Avatar asked Dec 16 '22 11:12

Pinu


1 Answers

SQL only:

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

PL/SQL:

previous_period := TO_CHAR(ADD_MONTHS(TO_DATE(current_period, 'YYYYMM'), -1), 'YYYYMM');
like image 159
Todd Gibson Avatar answered Jan 01 '23 00:01

Todd Gibson