ora-01841全年必须在4713和9999之间,而不是0

时间:2017-12-08 12:00:26

标签: oracle oracle11g oracle10g

我一直在处理一个令我发疯的查询,因为我似乎无法理解错误消息:我的查询是:

SELECT  MYTABLE." ID ", 


  NVL(max(TO_DATE(TO_CHAR(ADD_MONTHS(MYTABLE." XISSU_DT " ,MYTABLE." XTNR "), 'DD/MM/YYYY'),'DD/MM/YYYY')),  TO_DATE(SYSDATE , 'DD/MM/YYYY') )  MAXLASTINSDATE,

   TO_DATE(SYSDATE , 'DD/MM/YYYY'), 


      (TO_CHAR (TO_DATE(SYSDATE , 'DD/MM/YYYY')
           - TO_DATE(NVL(max(TO_DATE(TO_CHAR(ADD_MONTHS(MYTABLE." XISSU_DT " ,MYTABLE." XTNR "), 'DD/MM/YYYY'),'DD/MM/YYYY')),  TO_DATE(SYSDATE , 'DD/MM/YYYY') ) , 'DD/MM/YYYY')) * -1) MaturityPeriod


     FROM MYTABLE
where  
MYTABLE." STATUS " = 'A'
group by MYTABLE." ID "

我遇到的错误是:

ora-01841 full year must be between 4713 and 9999 and not be 0

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

TO_DATE(TO_CHAR(datevalue, 'DD/MM/YYYY'),'DD/MM/YYYY')正在删除与TRUNC( datevalue )实际上相同的任何时间组件。

然后TO_DATE(SYSDATE , 'DD/MM/YYYY')可能是您的错误所在,因为TO_DATE( stringvalue, format_model )将字符串作为第一个参数,因此您实际上是这样做的:

TO_DATE(
  TO_CHAR(
    SYSDATE,
    ( SELECT value FROM NLS_SESSION_PARAMETERS WHERE parameter = 'NLS_DATE_FORMAT' )
  ),
  'DD/MM/YYYY'
)

它没有意义,因为SYSDATE已经是DATE数据类型,因此您不需要使用TO_DATE

最后,TO_CHAR(SYSDATE - datevalue)*-1为什么要将它转换为字符串,然后将其乘以一个数字:

(SYSDATE - datevalue)*-1

但你甚至不需要*-1,因为你只需交换条款:

(datevalue - SYSDATE)

整理你需要的东西:

SELECT  MYTABLE." ID ", 
        NVL(
          MAX( TRUNC( ADD_MONTHS(MYTABLE." XISSU_DT " ,MYTABLE." XTNR ") ) ),
          SYSDATE
        ) MAXLASTINSDATE,
        SYSDATE, 
        ( NVL(
            MAX( TRUNC( ADD_MONTHS(MYTABLE." XISSU_DT " ,MYTABLE." XTNR ") ) ),
            SYSDATE
          )
          - SYSDATE
        ) AS MaturityPeriod
FROM   MYTABLE
where  MYTABLE." STATUS " = 'A'
group by MYTABLE." ID "