oracle decode 空值,常用函数--decode的使用,NULL值的意思

语法:

a6e20ebaccace55c6eb16358a7bd45f6.png

逻辑:

decode(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)

如果 条件值与值1相=,就返回 翻译值1

如果 条件值与值2相=,就返回 翻译值2

如果 条件值与值N相=,就返回 翻译值N

说明:返回值的数据类型就是翻译值的数据类型,缺省值可以省略,省略后返回NULL

DECODE适合的数据类型:numeric types (NUMBER, BINARY_FLOAT, or BINARY_DOUBLE) or character types

限制:search列表不能超过255个值

用法样例:

1.比较大小

select decode(sign(变量1-变量2),-1,变量1,变量2) from dual; --取较小值

sign()函数根据某个值是0、正数还是负数,分别返回0、1、-1

例如:

变量1=10,变量2=20

则sign(变量1-变量2)返回-1,decode解码结果为“变量1”,达到了取较小值的目的。

2.一般用法

SELECT product_id,

DECODE (warehouse_id, 1, 'Southlake',

2, 'San Francisco',

3, 'New Jersey',

4, 'Seattle',

'Non domestic')

"Location of inventory" FROM inventories

WHERE product_id < 1775;

3.表,视图结构转化

现有一个商品销售表sale,表结构为:

month    char(6)      --月份

sell    number(10,2)   --月销售金额

现有数据为:

200001  1000

200002  1100

200003  1200

200004  1300

200005  1400

200006  1500

200007  1600

200101  1100

200202  1200

200301  1300

想要转化为以下结构的数据:

year   char(4)      --年份

month1  number(10,2)   --1月销售金额

month2  number(10,2)   --2月销售金额

month3  number(10,2)   --3月销售金额

month4  number(10,2)   --4月销售金额

month5  number(10,2)   --5月销售金额

month6  number(10,2)   --6月销售金额

month7  number(10,2)   --7月销售金额

month8  number(10,2)   --8月销售金额

month9  number(10,2)   --9月销售金额

month10  number(10,2)   --10月销售金额

month11  number(10,2)   --11月销售金额

month12  number(10,2)   --12月销售金额

结构转化的SQL语句为:

create or replace view

v_sale(year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)

as

select

substrb(month,1,4), --year

sum(decode(substrb(month,5,2),'01',sell,0)), --month1

sum(decode(substrb(month,5,2),'02',sell,0)), --month2

sum(decode(substrb(month,5,2),'03',sell,0)), --month3

sum(decode(substrb(month,5,2),'04',sell,0)), --month4

......

sum(decode(substrb(month,5,2),'12',sell,0)), --month12

====================================================================================

问:什么是NULL?

答:在我们不知道具体有什么数据的时候,也即未知,可以用NULL,

我们称它为空,ORACLE中,含有空值的表列长度为零。

orACLE允许任何一种数据类型的字段为空,除了以下两种情况:

1、主键字段(primary key),

2、定义时已经加了NOT NULL限制条件的字段

说明:

1、等价于没有任何值、是未知数。

2、NULL与0、空字符串、空格都不同。

3、对空值做加、减、乘、除等运算操作,结果仍为空。

4、NULL的处理使用NVL函数。

5、比较时使用关键字用“is null”和“is not null”。

6、空值不能被索引,所以查询时有些符合条件的数据可能查不出来,

count(*)中,用nvl(列名,0)处理后再查。

7、排序时比其他数据都大(索引默认是降序排列,小→大),

所以NULL值总是排在最后。

使用方法:

SQL> select 1 from dual where null=null;

没有查到记录

SQL> select 1 from dual where null='';

没有查到记录

SQL> select 1 from dual where ''='';

没有查到记录

SQL> select 1 from dual where null is null;

1

---------

1

SQL> select 1 from dual where nvl(null,0)=nvl(null,0);

1

---------

1

对空值做加、减、乘、除等运算操作,结果仍为空。

SQL> select 1+null from dual;

SQL> select 1-null from dual;

SQL> select 1*null from dual;

SQL> select 1/null from dual;

查询到一个记录.

注:这个记录就是SQL语句中的那个null

设置某些列为空值

update table1 set 列1=NULL where 列1 is not null;

[注意:]查询空值记录:

SQL> select * from hrtest1 where salary=null;

no rows selected

SQL> select * from hrtest1 where salary is null;

FIRST_NAME LAST_NAME SALARY

-------------------- ------------------------- ----------

1 2

SQL>

参考:http://www.uusam.com/uu/blog/article.asp?id=15

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值