Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7575585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:43:06+00:00 2026-05-30T16:43:06+00:00

(moved from previous post) – sorry if this is seen as a repeat! Hi

  • 0

(moved from previous post) – sorry if this is seen as a repeat!
Hi everyone,
Just having some issues with date calculations and subtracting from a date within a function.

I am confused with the data types I should be using as I am having to convert from date to_char for example. I’m not sure whether to have the return type as varchar2 or date?

Here is the function that receives a car_id, looks in the table for that car, pulls out the cars arrive date, stores it in a date variable

does the same with the departure date.

Then converts both dates to_char, does the subtraction and returns it in a varchar2.

When I call the function it will inputting the car_id as a parameter,
then storing the returned result in a varchar variable, for a dbms output.
eg.

v_result := get_duration('0001')

here is the function:

DROP FUNCTION get_duration;  
CREATE FUNCTION get_duration (p_car_id number)  
RETURN varchar2 is  
v_arrive date;  
v_depart date  
v_duration varchar2(25); --not too sure about this variable choice  

begin  

select arrival, departure  
into v_arrive, v_depart
from car_info
where car_id = p_car_id;

v_duration := to_char(v_depart, 'dd-mon-yyyy hh24:mi:ss') - to_char(v_arrive, 'dd-mon-yyyy hh24:mi:ss')  
return v_duration; 
 END;  
/ 

As you can see, I am trying to put the start and end times from the table in to the variables, then minus the end date from the start date.

The function compiles, but with a warning, and when I got to call the function, the error: get_duration is invalid

Any input on this would be greatly received,

Sorry if the post is relatively big!

regards,

Darren

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:43:07+00:00Added an answer on May 30, 2026 at 4:43 pm

    Trivial problems are that you’re missing a ; when you define v_depart, and at the end of the line you assign the value to v_duration; and you’re mixing up your variable names. (You’re also inconsistent about the type of car_info.id; you’ve created it as a varchar when it probably ought to be a number, but that’s more of a comment on your previous question).

    The main problem is that you can’t perform a minus on two strings, as that doesn’t really mean anything. You need to do the manipulation of the original dates, and then figure out how you want to return the result to the caller.

    Subtracting one date from another gives a number value, which is the number of days; partial days are fractions, so 0.25 is 6 hours. With the dates from your previous quesiton, this query:

    select arrival, departure, departure - arrival as duration
    from car_info
    where car_id = 1;
    

    … shows duration of 2.125, which is 2 days and 3 hours.

    This isn’t the best way to do this, but to show you the process of what’s going on I’ll use that duration number and convert it into a string in quite a long-winded way:

    CREATE OR REPLACE FUNCTION get_duration (p_car_id number)
    RETURN varchar2 is
        v_arrive date;
        v_depart date;
        v_duration number;
        v_days number;
        v_hours number;
        v_minutes number;
        v_seconds number;
    BEGIN
    
        select arrival, departure, departure - arrival
        into v_arrive, v_depart, v_duration
        from car_info
        where car_id = p_car_id;
    
        -- Days is the whole-number part, which you can get with trunc
        v_days := trunc(v_duration);
        -- Hours, minutes and seconds are extracted from the remainder
        v_hours := trunc(24 * (v_duration - v_days));
        v_minutes := trunc(60 * (v_duration - v_days - (v_hours/24)));
        v_seconds := trunc(60 * (v_duration - v_days - (v_hours/24)
            - (v_minutes/(24*60))));
    
        return v_days || ' days '
            || to_char(v_hours, '00') || ' hours '
            || to_char(v_minutes, '00') || ' minutes '
            || to_char(v_seconds, '00') || ' seconds';
    END;
    /
    
    Function created.
    
    show errors
    
    No errors.
    
    select get_duration(1) from dual;
    
    GET_DURATION(1)
    --------------------------------------------------------------------------------
    2 days  03 hours  00 minutes  00 seconds
    

    You can play with the number format masks etc. to get the output you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just moved a web application to a windows server and having some
I have just moved from phase 1 to phase 2 of a project. In
I just moved from Visual Studio 2008 to Visual Studio 2010 (final) and noticed one major
I just started with WPF. Moved from Window Form. Where do those openDialog, saveDialog
I am currently using ASP.NET MVC 2.0 RC2, having recently moved from version 1.0.
Note that this is a follow-up question from my previous one: How to memorize
After having solved every of my previous issues to get the code running for
I recently had to re-factor some code from a previous maintainer and initially I
I have seen some code that removes a given layer from it's superlayer by
I'm having some issues with maintaining scroll bar locations. The Background I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.