今天遇到个很头疼的sql问题。。ORA-01722错误
数据库中有个字段QZFW起止范围是varchar类型,存放的数据格式是"数字编号"-"数字编号" 如:0012-1345 现在我想输入个条件a,使字段满足 QZFW中'-'前面的数字<= a <= QZFW中‘-’后面的数字。。
我写的sql如下:55000是条件a,,,
select * from (select to_number(substr(QZFW,0,instr(QZFW,'-')-1)) nbefore,
to_number(substr(QZFW,instr(QZFW,'-')+1,length(QZFW))) nafter,
QZFW as qzfw_C from TBL_01 Where translate(QZFW,'\1234567890-','\') is null and (length(QZFW) - length(replace(QZFW,'-')))/length('-')=1) t
where t.nbefore<=55000 and t.nafter >=55000
但是如果输入条件太大如:304000582 就会报“ORA-01722: 无效数字”错误。。。
把同样的数据导入到一个新建的表中。用同样的条件查询就没有问题,无语了。
insert into tt(id) select qzfw_c from (select to_number(substr(QZFW,0,instr(QZFW,'-')-1)) nbefore,
to_number(substr(QZFW,instr(QZFW,'-')+1)) nafter,
QZFW as qzfw_C from T_C602BC0C604B4 Where instr(QZFW,'-') > 1
and instr(QZFW,'-') < length(QZFW)and translate(QZFW,'\1234567890-','\') is null and (length(QZFW) - length(replace(QZFW,'-')))/length('-')=1) t
select * from (select to_number(substr(id,0,instr(id,'-')-1)) nbefore,
to_number(substr(id,instr(id,'-')+1)) nafter,
id as qzfw_C from tt Where instr(id,'-') > 1
and instr(id,'-') < length(id) and translate(id,'\1234567890-','\') is null and (length(id) - length(replace(id,'-')))/length('-')=1) t
where t.nbefore<=5501098080 and t.nafter>=5501098080
translate(id,'\1234567890-','\') is null 是确保id是中除了'-'就是数字。
(length(id) - length(replace(id,'-')))/length('-')=1 是确保id中有一个'-'字符。
最后没有使用数字比较,都转换为同长度的字符串,然后比较。。
select * from
(select QZFW as qzfw_C,
lpad((substr(QZFW, 0, instr(QZFW, '-') - 1)),13,'0') as nbefore,
lpad((substr(QZFW, instr(QZFW, '-') + 1)),13,'0') as nafter
from T_C602BC0C604B4
Where translate(QZFW, '\1234567890-', '\') is null
and (length(QZFW) - length(replace(QZFW, '-'))) / 1 = 1
) where nbefore<=lpad(550109808022,13,'0') and nafter>=lpad(550109808022,13,'0')