0

Oracle PL/SQL function that takes a string as a parameter.

What is the correct syntax for including this parameter in the Select portion of a statement.

The function is :

create or replace function GetHorizonTime(HorizonWindowMinutes IN VARCHAR2) 
return timestamp
IS
  RETVALUE TIMESTAMP;
BEGIN
  SELECT SYSDATE + Interval '''||HorizonWindowMinutes||''' MINUTE INTO RETVALUE
    FROM DUAL;
  RETURN RETVALUE;
END;
/

SELECT GetHorizonTime('720') FROM DUAL;

The select statement should translate to:

SELECT SYSDATE + Interval '720' MINUTE INTO RETVALUE
    FROM DUAL;
2
  • 1
    Why are you passing in a string? I would expect that HorizonWindowMinutes should be a number. Then you'd pass that number to the numtodsinterval function rather than constructing an interval literal. You could still do that if HorizonWindowMinutes is a string that only has numeric data using implicit conversion but that wouldn't generally make sense. Commented Jul 7, 2015 at 19:23
  • string was the latest incarnation trying to get it to work. I originally started with integer. Thanks, wasn't aware of the numtodsinterval function. Commented Jul 7, 2015 at 19:24

3 Answers 3

3
  create or replace function GetHorizonTime(HorizonWindowMinutes IN NUMBER) 
   return timestamp
   IS
     RETVALUE TIMESTAMP;
BEGIN
  RETURN (SYSDATE + HorizonWindowMinutes/1440);
END;
/
Sign up to request clarification or add additional context in comments.

Comments

2

Use the numToDSInterval function rather than constructing an interval literal

create or replace function GetHorizonTime(HorizonWindowMinutes IN NUMBER) 
  return timestamp
IS
BEGIN
  return SYSDATE + numtodsinterval( HorizonWindowMinutes, 'minute' );
END;
/

If you're working with timestamp data types, I would generally add an interval to the current_timestamp or localtimestamp rather than to a date like sysdate. If you don't need fractional seconds or time zones, on the other hand, it would seem sufficient to just return a date rather than a timestamp.

Comments

1

Another way of doing it is this one:

SELECT systimestamp  + HorizonWindowMinutes * Interval '1' MINUTE 
INTO RETVALUE
FROM DUAL;

Comments

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.