我创建了一个SQL脚本:

{CREATE TABLE ator (
id NUMBER(3) NOT NULL,
nome_artistico VARCHAR2(25) NOT NULL,
nss NUMBER(11),
sexo CHAR(1),
nacionalidade VARCHAR2(15),
email VARCHAR2(30),
telefone_fixo VARCHAR2(12),
telefone_movel VARCHAR2(12),
dt_nascimento DATE,
CONSTRAINT ator_pk PRIMARY KEY ( id )
);}我的问题是:有谁有关于如何计算和打印x演员的年龄的提示吗?我精通SQL,但不太擅长PL/SQL,所以如果有人能给我一些建议,我将不胜感激。
发布于 2021-01-06 23:40:43
您可以按如下方式使用months_between:
Select t.*, trunc(months_between(sysdate, dt_nascimento) / 12) as age
From ator t发布于 2021-01-07 02:35:08
如果您对PL/SQL解决方案感兴趣,我建议您使用函数:将actor的ID传递给它并返回actor的年龄。一个简单的例子:
SQL> create or replace function f_age (par_id in ator.id%type)
2 return number
3 is
4 l_age number;
5 begin
6 select round(months_between(sysdate, a.dt_nascimento) / 12)
7 into l_age
8 from ator a
9 where a.id = par_id;
10
11 return l_age;
12 end;
13 /
Function created.
SQL> select f_age(5) from dual;
F_AGE(5)
----------
54
SQL>https://stackoverflow.com/questions/65598545
复制相似问题