无效的数字错误 - [错误代码:1722,SQL状态:42000] ORA-01722:无效的数字

时间:2015-04-01 14:55:11

标签: oracle plsql

来自以下2个查询的第一个查询给我[错误代码:1722,SQL状态:42000] ORA-01722:无效数字错误。 但是当我限制第二个查询中的记录数时,它运行正常。 除了限制第二个查询中的行外,两个查询都是相同的。

SELECT b.first_name,
       b.last_name,
       b.device_derived,
       b.ios_version_group,
       b.add_date,
       FIRST_VALUE (b.add_date)
         OVER (PARTITION BY b.first_name, b.last_name, b.ios_version_group)
         AS first_date,
       LAST_VALUE (b.add_date)
         OVER (PARTITION BY b.first_name, b.last_name, b.ios_version_group)
         AS last_date
FROM   (SELECT a.first_name,
               a.last_name,
               a.os_version,
               a.device_type,
               a.device,
               a.add_date,
               a.device_derived,
               CASE
                 WHEN (    (   UPPER (a.device_derived) = 'IPHONE'
                            OR UPPER (a.device_derived) = 'IPAD')
                       AND TO_NUMBER (SUBSTR (a.os_version, 1, 1)) > 4)
                 THEN
                   'iOS ' || SUBSTR (a.os_version, 1, 1)
                 ELSE
                   'Others'
               END
                 AS ios_version_group
        FROM   (SELECT first_name,
                       last_name,
                       os_version,
                       device_type,
                       device,
                       add_date,
                       CASE
                         WHEN UPPER (device_type) = 'ANDROID'
                         THEN
                           'Android'
                         WHEN    UPPER (device_type) = 'BB'
                              OR UPPER (device_type) = 'BLACKBERRY'
                         THEN
                           'Blackberry'
                         WHEN     UPPER (device_type) = 'IOS'
                              AND (   SUBSTR (UPPER (device), 1, 6) = 'IPHONE'
                                   OR SUBSTR (UPPER (device), 1, 4) = 'IPOD')
                         THEN
                           'iPhone'
                         WHEN     UPPER (device_type) = 'IOS'
                              AND (SUBSTR (UPPER (device), 1, 4) = 'IPAD')
                         THEN
                           'iPad'
                       END
                         AS device_derived
                FROM   vw_mobile_devices_all) a) b;



SELECT b.first_name,
       b.last_name,
       b.device_derived,
       b.ios_version_group,
       b.add_date,
       FIRST_VALUE (b.add_date)
         OVER (PARTITION BY b.first_name, b.last_name, b.ios_version_group)
         AS first_date,
       LAST_VALUE (b.add_date)
         OVER (PARTITION BY b.first_name, b.last_name, b.ios_version_group)
         AS last_date
FROM   (SELECT a.first_name,
               a.last_name,
               a.os_version,
               a.device_type,
               a.device,
               a.add_date,
               a.device_derived,
               CASE
                 WHEN (    (   UPPER (a.device_derived) = 'IPHONE'
                            OR UPPER (a.device_derived) = 'IPAD')
                       AND TO_NUMBER (SUBSTR (a.os_version, 1, 1)) > 4)
                 THEN
                   'iOS ' || SUBSTR (a.os_version, 1, 1)
                 ELSE
                   'Others'
               END
                 AS ios_version_group
        FROM   (SELECT first_name,
                       last_name,
                       os_version,
                       device_type,
                       device,
                       add_date,
                       CASE
                         WHEN UPPER (device_type) = 'ANDROID'
                         THEN
                           'Android'
                         WHEN    UPPER (device_type) = 'BB'
                              OR UPPER (device_type) = 'BLACKBERRY'
                         THEN
                           'Blackberry'
                         WHEN     UPPER (device_type) = 'IOS'
                              AND (   SUBSTR (UPPER (device), 1, 6) = 'IPHONE'
                                   OR SUBSTR (UPPER (device), 1, 4) = 'IPOD')
                         THEN
                           'iPhone'
                         WHEN     UPPER (device_type) = 'IOS'
                              AND (SUBSTR (UPPER (device), 1, 4) = 'IPAD')
                         THEN
                           'iPad'
                       END
                         AS device_derived
                FROM   vw_mobile_devices_all) a) b
WHERE  ROWNUM <= 100;

有人可以告诉我为什么会收到此错误。有没有一种有效的方法来编写这个查询?

2 个答案:

答案 0 :(得分:3)

您的查询中有TO_NUMBER (SUBSTR (a.os_version, 1, 1),因此当您请求超过100行时,您可能会在os_version开头处找不到数字的数据。

您需要检查数据。

答案 1 :(得分:0)

尝试使用TO_NUMBER转换非数字值时会发生此错误。

在第二个查询中,前100行似乎不会导致a.os_version为非数字值。

尝试一个简单的选择vw_mobile_devices_all来查找非数字os_version。弄清楚如何解决问题。也许你可以用不同的方式查询os_version。