代码改变世界

oarcle 常用函数

2008-08-07 09:24  Jeffery Tao  阅读(449)  评论(0)    收藏  举报

1、nvl

 

通过查询获得某个字段的合计值,如果这个值位null将给出一个预设的默认值
select nvl(sum(t.dwxhl),1) from tb_jhde t where zydm=-1这里关心的nvl的用法,nvl(arg,value)代表如果前面的arg的值为null那么返回的值为后面的value

另一个有关的有用方法
declare
i integer
select nvl(sum(t.dwxhl),1) into i from tb_jhde t where zydm=-1
这样就可以把获得的合计值存储到变量i中,如果查询的值为null就把它的值设置为默认的1

 

2、

SUBSTR(string,start,count)
取子字符串,从start开始,取count个

代码示例如下:
SQL> select substr('13088888888',3,8) from dual;

 

结果显示:

SUBSTR('
--------
08888888

 

3、FLOOR


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

结果如下:

FLOOR(2345.67)
--------------

4、SYSDATE
用来得到系统的当前日期
SQL> select to_char(sysdate,'dd-mm-yyyy day') from dual;

TO_CHAR(SYSDATE,'
-----------------
09-05-2004 星期日
trunc(date,fmt)按照给出的要求将日期截断,如果fmt='mi'表示保留分,截断秒
SQL> select to_char(trunc(sysdate,'hh'),'yyyy.mm.dd hh24:mi:ss') hh,
  2  to_char(trunc(sysdate,'mi'),'yyyy.mm.dd hh24:mi:ss') hhmm from dual;


 

HH                  HHMM
------------------- -------------------
2004.05.09 11:00:00 2004.05.09 11:17:00



          2345

 

5、TO_CHAR(date,'format')
SQL> select to_char(sysdate,'yyyy/mm/dd hh24:mi:ss') from dual;

TO_CHAR(SYSDATE,'YY
-------------------
2004/05/09 21:14:41

 

6、TO_DATE(string,'format')
将字符串转化为ORACLE中的一个日期
7、TO_NUMBER
将给出的字符转换为数字
SQL> select to_number('1999') year from dual;

     YEAR
---------
     1999

8、GREATEST
返回一组表达式中的最大值,即比较字符的编码大小.
SQL> select greatest('AA','AB','AC') from dual;

GR
--
AC
SQL> select greatest('啊','安','天') from dual;


 

GR
--


 


LEAST
返回一组表达式中的最小值
SQL> select least('啊','安','天') from dual;


 

LE
--

 

9、HAVING
对分组统计再加限制条件
SQL> select deptno,count(*),sum(sal) from scott.emp group by deptno having count(*)>=5;

   DEPTNO  COUNT(*)  SUM(SAL)
--------- --------- ---------
       20         5     10875
       30         6      9400
SQL> select deptno,count(*),sum(sal) from scott.emp having count(*)>=5 group by deptno ;


 

   DEPTNO  COUNT(*)  SUM(SAL)
--------- --------- ---------
       20         5     10875
       30         6      9400

 

10、MAX(DISTINCT|ALL)
求最大值,ALL表示对所有的值求最大值,DISTINCT表示对不同的值求最大值,相同的只取一次
SQL> select max(distinct sal) from scott.emp;

MAX(DISTINCTSAL)
----------------
            5000


 


MIN(DISTINCT|ALL)
求最小值,ALL表示对所有的值求最小值,DISTINCT表示对不同的值求最小值,相同的只取一次
SQL> select min(all sal) from gao.table3;


 

MIN(ALLSAL)
-----------
    1111.11


11、UID
返回标识当前用户的唯一整数
SQL> show user
USER 为"GIS"
SQL> select username,user_id from dba_users where user_id=uid;

USERNAME                         USER_ID
------------------------------ ---------
GIS                                   25