Oracle 内建函数和connect by使用
一、Oracle 内建函数greatest()、least()、to_single_byte()使用:
1、取一个一维数组中的最大值greatest
SQL> SELECT Greatest('a','x','fit','xa') FROM dual;
GR
--
xa
SQL> select greatest(1,3,5,6,8,3,2,1) from dual;
GREATEST(1,3,5,6,8,3,2,1)
-------------------------
8
2、取一个一维数组中的最小值least
SQL> SELECT Least('a','x','fit','xa','ab') FROM dual;
L
-
a
SQL> select greatest(1,3,5,6,8,3,2,1) from dual;
GREATEST(1,3,5,6,8,3,2,1)
-------------------------
8
SQL> select least(1,3,5,6,8,3,2,1) from dual;
LEAST(1,3,5,6,8,3,2,1)
----------------------
1
3、全角的数字/字母/标点符号转半角to_single_byte
SQL> SELECT To_Single_Byte('21,500円(消費税別)US$@¥55/pc') half_width FROM dual;
HALF_WIDTH
-------------------------------------------------------------------
21,500円(消費税別)US$@¥55/pc
4、半角转全角
SQL> SELECT to_multi_byte('21,500円(消費税別)US$@¥55/pc') full_width FROM dual;
FULL_WIDTH
--------------------------------------------------------------------------
21,500円(消費税別)US$@¥55/pc
二、connect by rownum使用
在做报表的时候需要使用日期序列时,通常的做法就是创建一张日期表,今天获得一个新方法:即使用connect by 来产生一个日期序列,这样就可以不用再去创建日期表了。具体用法:
SQL> select to_date('20120101','yyyymmdd')+rownum rn from dual connect by rownum <= 10 ;
RN
----------
2012-01-02
2012-01-03
2012-01-04
2012-01-05
2012-01-06
2012-01-07
2012-01-08
2012-01-09
2012-01-10
2012-01-11
10 rows selected.
SQL> select to_date('20121231','yyyymmdd')-rownum rn from dual connect by rownum <= 10 ;
RN
----------
2012-12-30
2012-12-29
2012-12-28
2012-12-27
2012-12-26
2012-12-25
2012-12-24
2012-12-23
2012-12-22
2012-12-21
10 rows selected.
另外,connect by也可以产生一个依次递减的整数序列:
SQL> select rownum rn from dual connect by rownum <= 10 ;
RN
----------
1
2
3
4
5
6
7
8
9
10
10 rows selected.