Search code examples
plsqloracle-apexsysdate

PL/SQL sysdate format not working?


I have a problem with sysdate formatting in PL/SQL. I try to calculate my age, upon the day (so it should return something like 20.44). However sysdate won't work with this format and the value v_leeftijd keeps returning -2000.91. Obvious that isn't my age. How do I solve this? I have the following code:

declare
v_vandaag date := to_date(sysdate, 'DD-MM-YYYY');
v_geboortedatum date := to_date('29-05-2014', 'DD-MM-YYYY');
v_leeftijd number(10,2);

begin

--leeftijd/dagen = leeftijd in jaren
v_leeftijd := (v_vandaag - v_geboortedatum)/365;

dbms_output.put_line(v_leeftijd);

end;

Solution

  • sysdate is already a date:

    SET SERVEROUTPUT ON
    declare
        v_geboortedatum date := to_date('29-05-2014', 'DD-MM-YYYY');
        v_leeftijd number(10,2);
    begin
        --leeftijd/dagen = leeftijd in jaren
        v_leeftijd := (sysdate - v_geboortedatum)/365;
    
        dbms_output.put_line(v_leeftijd);
    end;
    /