1

A person rents a car today and for 7 days. The function will return: date + hours + minutes. Thanks in advance! : )

create or replace function get_return_date(
p_days in number)
return varchar2
is 
begin
return to_char(sysdate,'YYYY-MM-DD:HH24:MI') + p_days;
end;
/
6
  • Why do I get a error when a take Commented Oct 13, 2017 at 13:13
  • Error if try to select get_return_date(7) from dual; Commented Oct 13, 2017 at 13:14
  • Is there some specific reason you are returning string instead of a date? Commented Oct 13, 2017 at 14:55
  • Is it the same answer with date? Commented Oct 13, 2017 at 16:03
  • I tried with to_date and then I did get the date back, not the time. Commented Oct 13, 2017 at 16:50

2 Answers 2

1

Add the days before converting to a string:

create or replace function get_return_date(
p_days in number)
return varchar2
is 
begin
    return to_char(sysdate + p_days,'YYYY-MM-DD:HH24:MI');
end;
/
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot : ): ): )
0

The reason your code is not working is because you need to do the math before you convert to a string. Essentially, this is what you are telling the compiler:

"Monday" + 2

That doesn't make any sense in math terms, so it throws an error. the sysdate is actually already a number, so you can add another number to it. Then, you use your to_char to convert that single value into a formatted string like this:

create or replace function get_return_date(
p_days in number)
return varchar2
is 
begin
return to_char(sysdate + p_days,'YYYY-MM-DD:HH24:MI');
end;
/

2 Comments

Thanks a lot, it make sense to me now! : ) : )
No problem. If one of these answers solved your problem, please mark it as accepted to indicate to other users your issue was resolved.

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.