尝试从 table 中的日期获取年龄,但没有成功。错误 "ORA-00923: FROM keyword not found where expected"
Try to get age from date in table but did not work. Error "ORA-00923: FROM keyword not found where expected"
我尝试 运行 并从 table 中的日期获取年龄,但最终出现错误“ORA-00923:未在预期位置找到 FROM 关键字”。
这是我第一次 运行 在 apex oracle
运费table
ship_id ship_date
SEL0001 12/6/2015
SEL0002 01/5/2016
sql代码
select ship_id, YEAR(ship_date) AS [BirthDate],
YEAR(GETDATE()) AS [ThisYear],
YEAR(GETDATE()) -YEAR(ship_date) AS Age
from shipping
预期输出
ship_id BirthDate ThisYear Age
SEL0001 12/6/2015 2020 5
SEL0002 01/5/2016 2020 4
有什么办法可以得到年龄吗?我尝试使用 datediff 但我不是这方面的专家。谢谢。
你应该在 Oracle 中使用 extract
,这里是 demo。
select
ship_id,
TO_CHAR(ship_date , 'MM/DD/YYYY') as BirthDate,
extract(YEAR from sysdate) as ThisYear,
extract(YEAR from sysdate) - extract(YEAR from ship_date) as Age
from shipping
输出:
*---------------------------------------*
| SHIP_ID BIRTHDATE THISYEAR AGE |
*---------------------------------------*
| SEL0001 12/06/2015 2020 5 |
| SEL0002 01/05/2016 2020 4 |
*---------------------------------------*
使用 extract
获取年或月值,对于年龄,使用 `months_between' 获取 no.of 个月然后计算年龄。
select ship_id,
to_char(ship_date,'DD/MM/YYYY') "BirthDate",
extract(year from SYSDATE) ThisYear,
months_between(sydate,ship_date)/12 as Age_inYears
from shipping
我尝试 运行 并从 table 中的日期获取年龄,但最终出现错误“ORA-00923:未在预期位置找到 FROM 关键字”。
这是我第一次 运行 在 apex oracle
运费table
ship_id ship_date
SEL0001 12/6/2015
SEL0002 01/5/2016
sql代码
select ship_id, YEAR(ship_date) AS [BirthDate],
YEAR(GETDATE()) AS [ThisYear],
YEAR(GETDATE()) -YEAR(ship_date) AS Age
from shipping
预期输出
ship_id BirthDate ThisYear Age
SEL0001 12/6/2015 2020 5
SEL0002 01/5/2016 2020 4
有什么办法可以得到年龄吗?我尝试使用 datediff 但我不是这方面的专家。谢谢。
你应该在 Oracle 中使用 extract
,这里是 demo。
select
ship_id,
TO_CHAR(ship_date , 'MM/DD/YYYY') as BirthDate,
extract(YEAR from sysdate) as ThisYear,
extract(YEAR from sysdate) - extract(YEAR from ship_date) as Age
from shipping
输出:
*---------------------------------------*
| SHIP_ID BIRTHDATE THISYEAR AGE |
*---------------------------------------*
| SEL0001 12/06/2015 2020 5 |
| SEL0002 01/05/2016 2020 4 |
*---------------------------------------*
使用 extract
获取年或月值,对于年龄,使用 `months_between' 获取 no.of 个月然后计算年龄。
select ship_id,
to_char(ship_date,'DD/MM/YYYY') "BirthDate",
extract(year from SYSDATE) ThisYear,
months_between(sydate,ship_date)/12 as Age_inYears
from shipping