1

I have the following (private) PL/SQL function (inside PACKAGE) returning the previous month in 'YYYYMM' format:

FUNCTION GET_PREV_MONTH RETURN VARCHAR2 IS
BEGIN
  RETURN TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYYMM');
END GET_PREV_MONTH;

I need to compare if records are from previous month in several my queries so I wanted to make private function returning previous month. Why I cannot use this return value from GET_PREV_MONTH() function in WHERE clause of my other SQL queries like:

UPDATE mytable t SET t.col=1
WHERE TO_CHAR(t.created,'YYYYMM')=GET_PREV_MONTH();

Oracle says PLS_00231: function 'GET_PREV_MONTH' may not be used in SQL.

If I create this, this works!

UPDATE mytable t SET t.col=1
WHERE TO_CHAR(t.created,'YYYYMM')=TO_CHAR(ADD_MONTHS(SYSDATE,-1),'YYYYMM');
7
  • Please show us the content of GET_PREV_MONTH function. Commented May 4, 2017 at 9:31
  • You don't see my function code above in thread? Commented May 4, 2017 at 10:02
  • Sorry, I had to miss it. Commented May 4, 2017 at 11:49
  • Don't compare dates in string form, compare dates as dates. If the column is a date and it is indexed you will lose the ability to make use of the index. WHERE t.created >= TRUNC(ADD_MONTHS(SYSDATE,-1)) AND t.created < TRUNC(ADD_MONTHS(SYSDATE,-1)) + 1 Commented May 4, 2017 at 14:28
  • 1
    You are correct, to cover the entire month, adjust the from and to dates like the following WHERE t.created >= TRUNC(ADD_MONTHS(SYSDATE,-1),'MON') -- greater than the start of the previous month AND t.created < TRUNC(SYSDATE, 'MON') -- less than the start of the current month Commented May 5, 2017 at 14:49

1 Answer 1

1

I wanted to make private function returning previous month. Why I cannot use this return value from GET_PREV_MONTH() function in WHERE clause of my other SQL queries

It is a private function - you cannot reference it outside the package.

If you want to use it in SQL then declare it in the package specification so that it is public.

This is private function of this package! This function is used in procedures from the same package. It is not referenced outside this package.

It CANNOT be used in the SQL context if it is not a public function; regardless of whether the SQL is being called from inside or outside the same package.

Sign up to request clarification or add additional context in comments.

4 Comments

To expand on your answer : stackoverflow.com/questions/16130165/…
This is private function of this package! This function is used in procedures from the same package. It is not referenced outside this package.
@sbrbot That makes no difference. You can use it in a PL/SQL context within the same package but if you are using it in an SQL context then the function must be public (regardless of whether that SQL context is instantiated from a PL/SQL context inside the package).
OK, thanks, I was not aware of different contexts (PL/SQL and SQL contexts). I will check this now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.