1.1 oracle中decode函数
含义解释: decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值)
select a/b from table1 t1; --当b为0的时候,oracle会提示除数为0,而我们一般希望除数为0时,直接输出0即可。 --解决方法: select decode(b,0,0,a/b) from table1 t1; --即当b = 0时,返回0,否则才返回a/b的结果。
2.1 sql server中的nullif函数
含义解释:nullif(expression,expression) 两表达式相同,则返回null
--解决方法: select isnull(a/nullif(b,0),0) from table2 t2; --当除数b为0时,除数变为null,则a/b也为null,再通过isnull函数将null转为0.
3.1通过case... when...
--解决方法: select case when b=0 then 0 else a/b end from table t3 --当除数b等于0时,直接返回0,否则再返回a/b