简体   繁体   English

如何从 oracle sql 中的最大日期获取年份

[英]how to get the year from the max date in oracle sql

I am trying to get yyyy from the below query it is throwing error if i try to use the我正在尝试从以下查询中获取 yyyy,如果我尝试使用

Query询问

WITH last_startdate
     AS (SELECT owner,
                package_name,
                Max(start_date) MAX_START_DATE,
                to_date(max(start_date),'YYYY') as max_start_date_yr
         FROM   (SELECT DISTINCT owner,
                                 object_name AS PACKAGE_NAME
                 FROM   dba_procedures

                ) P
                JOIN SCHEMA.table_name A /*AUDITING TABLE*/
                  ON P.package_name = A.task_name
         GROUP  BY P.owner,
                   P.package_name)
SELECT *
FROM   last_startdate
ORDER  BY 3 DESC 

Error错误

ORA-01830: date format picture ends before converting entire input string
01830. 00000 -  "date format picture ends before converting entire input string"
*Cause:    
*Action:

Expected Result预期结果

OWNER PACKAGE_NAME MAX_START_DATE MAX_START_DATE_YR
STAGE PA_AB_CDS    19.02.2021     2021
EDW   PA_BX_BCS    09.12.2020     2020
MART  PA_WQ_AFD    12.08.2019     2019
CODE  PA_WQ_IOD    23.05.2016     2016

any suggestion to get the expected results ?有什么建议可以得到预期的结果吗?

start_date is a DATE I suppose.我想start_date是一个DATE So max(start_date) is a date, too.所以max(start_date)也是一个日期。

Why then do you apply to_date on it?那你为什么要申请to_date呢? Convert the date into a date?将日期转换为日期? That makes no sense.这是没有意义的。 You my want want to_char instead你我想要to_char代替

to_char(max(start_date),'YYYY') as max_start_date_yr

which would give you the year in a string.这会给你一个字符串的年份。 Or just use EXTRACT to get the numeric year:或者只使用EXTRACT来获取数字年份:

extract(year from max(start_date)) as max_start_date_yr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM