错误“ORA-01830”:日期格式图片在转换整个输入字符串之前结束(Oracle错误)

时间:2015-09-15 08:07:53

标签: oracle date datetime

请帮忙,这是我的疑问:

SELECT   a.verifierid, a.peerverifier, periode AS last_periode,
         endToEndDelayMedian / 1000 AS x_medianroundtriplatency,
         medianJitter / 1000 AS x_maxjitter, percentLostPackets,
         CASE
WHEN (endToEndDelayMedian / 1000) < 80
AND (endToEndDelayMedian / 1000) > 0 THEN
    1
WHEN 80 <= (endToEndDelayMedian / 1000)
AND (endToEndDelayMedian / 1000) < 100 THEN
    2
ELSE
    3 end as sign_latency,
         CASE
            WHEN (medianJitter / 1000) < 5
            AND (medianJitter / 1000) > 0
               THEN 1
            WHEN 5 <= (medianJitter / 1000)
            AND (medianJitter / 1000) < 30
               THEN 2
            ELSE 3
         END AS sign_jitter,
         CASE
            WHEN percentLostPackets < 0.5
               THEN 1
            WHEN 0.5 <= (percentLostPackets)
            AND (percentLostPackets) < 1
               THEN 2
            ELSE 3
         END AS sign_packetlost,
         last_update
    FROM (select to_char(case when periode like '201%' then to_date(periode,'yyyy-mm-dd hh24:mi:ss')
else to_date(periode,'mm-dd-yyyy hh24:mi:ss') end,'YYYY-MM-DD hh24:mi:ss') periodex, a.*
from nextone_brix_l3vpn a ) a
         INNER JOIN
         (SELECT   verifierid, peerverifier, MAX (periode) AS last_periode
              FROM nextone_brix_l3vpn
          GROUP BY verifierid, peerverifier) b
         ON a.verifierid = b.verifierid
       AND a.peerverifier = b.peerverifier
       AND periode = b.last_periode
where 1=1  and to_char(periodex,'yyyy-mm-dd hh24:mi:ss') between SYSDATE - INTERVAL '24' HOUR AND SYSDATE and rownum<=10
ORDER BY sign_packetlost DESC,
         sign_latency DESC,
         sign_jitter DESC,
         verifierid ASC
  

错误“ORA-01830”:日期格式图片在转换整个之前结束   输入字符串(Oracle错误)

2 个答案:

答案 0 :(得分:1)

你的问题出在日期

 to_date(periode,'yyyy-mm-dd hh24:mi:ss')
    else to_date(periode,'mm-dd-yyyy hh24:mi:ss') end,'YYYY-MM-DD hh24:mi:ss') periodex, a.*
    from nextone_brix_l3vpn a ) a

期间的价值是多少?它应该像这样'15/09/2015 00:00:00'

示例:select To_date ('15/09/2015 00:00:00', 'DD/MM/YYYY HH24:MI:SS') From DUAL;

答案 1 :(得分:0)

您已指定PERIODE中的数据可以是YYYY-MM-DD HH24:MI:SSMM-DD-YYYY HH24:MI:SS

在您的数据中某处,此预期格式未成立。

你可以用以下方法测试:

SELECT to_char(
           case 
              when periode like '201%' then to_date(periode,'yyyy-mm-dd hh24:mi:ss')
              else to_date(periode,'mm-dd-yyyy hh24:mi:ss') 
            end,
        'YYYY-MM-DD hh24:mi:ss') periodex
  FROM nextone_brix_l3vpn;

运行此操作会导致您在原始代码中看到ORA-01830错误。

要确定您的错误记录的位置,请尝试以下操作:

SET SERVEROUTPUT ON

DECLARE
   v_converted_date DATE;
BEGIN
   -- Enable the DBMS_OUTPUT buffer
   DBMS_OUTPUT.ENABLE(1000000);

   -- Start your output
   DBMS_OUTPUT.PUT_LINE('Starting loop');

   -- Loop through your data table
   FOR i IN (SELECT rowid, periode FROM nextone_brix_l3vpn)
   LOOP
      -- Start a new Pl/SQL block
      BEGIN
        -- Try your data conversion
        v_converted_date := CASE
                               WHEN i.periode LIKE '201%' 
                               THEN TO_DATE(i.periode,'yyyy-mm-dd hh24:mi:ss')
                               ELSE TO_DATE(i.periode,'mm-dd-yyyy hh24:mi:ss') 
                             END;
      EXCEPTION
         -- Trap the exception and output the rowed of the problem record
         WHEN others
         THEN
            DBMS_OUTPUT.PUT_LINE('Error: '||sqlcode||', caused by record at row: '||i.rowid||' with PERIODE value: '||i.periode);
      END;
   END LOOP;

   -- End your output
   DBMS_OUTPUT.PUT_LINE('Loop finished');
EXCEPTION
   -- Trap any other exceptions and report them
   WHEN others
   THEN
      DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;

然后应该输出导致格式化问题的记录列表。

Caveats: 
  1. 我没有测试此代码的SQL会话,因此您可能需要稍微破解它以使其正常工作
  2. 除了使用DBMS_OUTPUT之外,还有其他方法可以输出记录,所以请随意这样做,但对您来说最简单
  3. 是的,这意味着您必须运行65m记录中的任何一条,并且有人可能会建议更快的纯SQL方法来识别问题记录
  4. 出于这个原因,您不应该将数据库中的日期存储为VARCHAR,如果是日期,请将其存储在DATE数据类型中!
  5. 希望它有所帮助!