数据库中有个字段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: 无效数字”错误
我写的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: 无效数字”错误
另外,这个取后面部分的数字,不需要length(QZFW)
to_number(substr(QZFW,instr(QZFW,'-')+1))
是否存在:
-8345
345-
这样的数据?????试下增加条件:
and instr(QZFW,'-') > 1
and instr(QZFW,'-') < length(QZFW)
加上条件了,数小了可以,打了还是报错。
select * 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
where t.nbefore<=222 and t.nafter >=222
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
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')