请教sql decode函数的一个简单用法
来源:百度知道 编辑:UC知道 时间:2024/05/25 12:27:56
执行如下sql:
select value from table where id=1
查询出来的值可能大于0也可能小于0。我现在想把小于0的结果全都改成0输入。这个用decode函数怎么做的?
好像是select decode(value...?) from table where id=1
select case when value<=0 then 0 else value end from table where id=1
那如果在一个查询语句中同时查询两个安段的情况应该怎么写呢?
比如:
select a,b from table,对b进行判断
select value from table where id=1
查询出来的值可能大于0也可能小于0。我现在想把小于0的结果全都改成0输入。这个用decode函数怎么做的?
好像是select decode(value...?) from table where id=1
select case when value<=0 then 0 else value end from table where id=1
那如果在一个查询语句中同时查询两个安段的情况应该怎么写呢?
比如:
select a,b from table,对b进行判断
首先decode函数是Oracle独有的判断函数!
//其实就是一个简单的判断函数,比如字段的值为a
decode(字段,'a','输出是a','是a','输出是b','否则输出都不是');
//下面我拿EMP表的SCOTT来示例代码:
SELECT
DECODE(ename,'是SCOTT','Yes','不是SCOTT','No','都不是')
FROM emp
WHERE ename = UPPER('scott');
不用这个函数也可以实现:
select case when value<=0 then 0 else value end from table where id=1
select case when 字段<0 then 0 else 字段 end 要显示成的字段名 from table where id=1
sql2000中可以用case这么实现 decode我没用过