Trying to solve one assignment where I have a table consisting of owners of cars. I am supposed to write an anonymous PLSQL statement where I should print first name, last name and how old the owners are, like if their dob is '19801109' it should convert that to their age in a number, with one decimal, like '34,4 years old'. How to do that? (very new to this as you can imagine). Here is my code, without any function for the conversion/calculation:
declare
cursor c_owners is select initcap(fname), initcap(lname), dob
from car_owner;
v_dob car_owner.pnr%type;
v_fnamn car_owner.fname%type;
v_enamn car_owner.lname%type;
begin
if not c_owners%isopen then
open c_owner;
end if;
loop
fetch c_owner
into v_fname, v_lname, v_dob;
exit when c_owners%notfound;
dbms_output.put_line(v_fname||', '||v_lname||', '||v_dob||'year.');
end loop;
close c_owners;
end;
/
Would be awesome if someone could help. Been stuck with this for some time now. I know I could use better ways of writing to get less code but trying to learn the basics first :) The result should look like:
John, Johnson, 34,4 years old.
When I asked a friend he had done like this:
begin
for rec in select fname, lname, round (months_between(sysdate, to_Date('19'||
SUBSTR(dob,0), INSTR(dob, '-')-1)'YYMMDD')) as age
from car_owner;
loop dbms_output.put_line(initcat(rec.fname) || ', '|| initcap(rec.lname) ||' , '|| rec.age||' age';
end loop;
end;
/
But that did not work (annoying right parentheses error no matter how I did), and I did not understand it fully, but maybe it helps you see how it should look? its about ten rows in the table and the output should print each ones age based on their dob.