现在的位置: 首页 > 综合 > 正文

ORACLE函数使用

2012年11月04日 ⁄ 综合 ⁄ 共 2087字 ⁄ 字号 评论关闭

--如果WARE_ID 没有数据,则为1
SELECT nvl(a.WARE_ID,1) FROM DTF_TEST a;

--移动表到哪个分区 tablename 表名 new_name 分区名--
alter table tablename move 
TABLESPACE new_name 
Storage(initial 50M next 50M 
pctincrease 0 pctfree 10 pctused 50 initrans 2) NOLOGGING;

--移动分区的语法 
alter table tablename move (partition partname) 
[update global indexes] 

--如果为空 就不拼接--
select concat(WARE_ID,'88888888')||'转23'  丁腾飞电话 from DTF_TEST;

--首字母大写
select initcap(WARE_NAME) 大写 from DTF_TEST;

--返回字符串,并将所有的字符小写
select lower('AaBbCcDd') 小写 from dual;

--返回字符串,并将所有的字符大写
select upper('AaBbCcDd') 大写 from dual;

--LTRIM  删除左边出现的字符串
--RTRIM  删除右边出现的字符串
select ltrim(rtrim('   gao qian jing   ',' '),' ') from dual;

--取子字符串,从start开始,取count个  08888888
select substr('13088888899',3,8) from dual;

--string   希望被替换的字符或变量 
--s1       被替换的字符串
--s2       要替换的字符串
select replace('he love you','he','i') from dual;

--对给定的数字取整数
select floor(2345.67) from dual;

--四舍五入--
SELECT ROUND(1.56), ROUND(1.56,1), ROUND(12.34, -2) FROM dual

NVL   NVL(A,B) 如果A是空,那么返回B 如果A不是空,返回A自己
NVL2  NVL2(A,B,C)  如果A不是空,则是B  如果A是空,则返回C 

-----------------
select nvl(0,f()) from dual; 0不是空,所以仍然等于0  实际上,仍然触发了F()       也就是NVL里的所有表达式,不管是否满足,都必须执行

select nvl2(0,'not null',f()) from dual; 0不是空,所以直接返回NOT NULL     仍然执行了f()      也就是NVL和NVL2里的任何表达式,都必须执行
-----------------
select nvl2(0,'not null','null') from dual; 0如果不是空,显示第2个表达式"not null"

select coalesce(0,f()) from dual; coalesce(0,f())是显示第1个非空表达式,0就是非空,那f()会执行吗? 跟NVL一样,也强制执行了f()   coalesce 9I会计算F() 10G的不计算F()
-----------------
select coalesce(null,1,2,3,4) from dual; coalesce(null,1,2,3,4)是返回第一个非空的表达式    这里第一个非空是1

select decode(0,0,'不执行f()',f()) from dual;只执行满足的表达式

select decode( i, 5, 'five', 6, 'six', 'Else I just don''t know!' )    from t;  4,5,6 而4没列在DECODE中
所以显示Else I just don''t know!

//如果有符合的数据,就填充,没有,则,dd
SELECT DECODE(t.a,1,'one',2,'two',3,'three','dd') a FROM t;

//求出最大值和最小值的时候,如果是单个SQL语句去取,则需要扫描整个索引
//如果我们用两个SQL子句并行去取一个MAX,一个MIN
1.select min(created),max(created) from big_table;

//效率更好
2.select min(created), max(created)                       
   from (                                               
          select min(created) created from big_table    
          union all                                     
          select max(created) created from big_table    
        )  

//所有数据中,每行最大的数据
select t.*, greatest( a,b,c) row_max from t;

select nvl2(0,'not null','null') from dual;

select coalesce(0,f()) from dual; 

抱歉!评论已关闭.