繁体   English   中英

Oracle比较varchar2字段和数字

[英]Oracle Comparing varchar2 field with number

我有一个查询,其中有以下子查询

(select account from asl_data where alias_accunt = trim(field) )

其中alias_account是varchar2(20),而字段值来自另一个子查询的substr。 但是的值为00123456789

为此,我收到错误作为无效数字。 我尝试了to_char,在两个字段上都强制转换了函数。 但是似乎没有任何作用。

我想念什么吗? 有什么建议吗?

谢谢!!

在此查询/过程的某处,尝试将字符串隐式转换为数字。 可能在子查询中,或者可能是因为变量声明是其中一个变量的数字。 不过,您发布的示例似乎没有错。

识别字段后,您可以使用类似的查询查看导致此问题的行。

with t1 as 
(
    select '100' id1, 'first' name1 from dual
    union all
    select '100A' id1, 'second' name1 from dual
    union all
    select '$$1' id1, 'third' name1 from dual
)
select * from t1

100    first
100A   second
$$1    third

如果您尝试将id转换为数字,则第一个将可用(也许您的示例就是这种情况),但是其他会引发错误。

with t1 as
(
    select '100' id1, 'first' name1 from dual
    union all
    select '100A' id1, 'second' name1 from dual
    union all
    select '$$1' id1, 'third' name1 from dual
)
select id1,
       name1,
       to_number(id1)
   from t1

/

ERROR:
ORA-01722: invalid number

要确定有此问题的行,请使用...

with t1 as 
(
    select '100' id1, 'first' name1 from dual
    union all
    select '100A' id1, 'second' name1 from dual
    union all
    select '$$1' id1, 'third' name1 from dual
)
select id1, 
       name1
       --,replace( translate( id1, '0123456789', '0000000000' ), '0', '' ),
       --length(replace( translate( id1, '0123456789', '0000000000' ), '0', '' ))
   from t1
   where length(replace( translate( id1, '0123456789', '0000000000' ), '0', '' )
               ) <> 0

id1     name1
---------------
100A    second
$$1     second

暂无
暂无

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

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