有表table,字段vdef1(varchar类型,默认值是'~'),字段vdef2(number类型)
需求是:将表table中vdef2-vdef1=0的所有信息查询出来
我的思路是:
select * from table where vdef2-vdef1=0;
但是会报错,因为vdef1的默认值是“~”,所以要把这个默认值替换成0才能计算。但是不会写SQL,所以求助各位大神
问题是:怎么写这条语句?怎么写这条语句?怎么写这条语句?
select * from table where decode(vdef2,'~',0,vdef2) - vdef1 = 0--如果vdf2的值为~,则decode的最后结果是0,否则就是vdef2
如果好使,可以查一下decode函数,现在基本用case when 来替代decode了
改进:select * from table where (case when vdef2 LIKE '%[^0-9]%' then cast(vdef2 as decimal(18, 2)) else 0 end)-vdef1=0