Oracle函数学习

函数:

concat(str1,str2)

例子:字符串拼接函数 
select concat('Hello ','World') from dual;

length(str)

例子:表达式中的字符数
select length('Hello World!') from dual;--返回结果为12

nvl(x,value)

例子:将一个NULL转换为另外一个值,如果x为NULL,则返回value,否则返回x值本身
select nvl(address,'北京市') from student;

nvl2(x,value1,value2)

例子:如果x不为NULL,返回value1,否则,返回value2
select nvl2(address,'有地址','无地址') from student;

replace(x,search_string,replace_string)

例子:从字符串x中搜索search_string字符串,并使用replace_string字符串替换。并不会修改数据库中原始值
select replace('Hello World!','o','HA') from dual;

substr(x,start[,length])

例子:返回字符串中的指定的字符
select substr('Hello World',3) from dual; --返回结果为'llo World'
select substr('Hello World',-3) from dual;--返回结果为'rld'
select substr('Hello World',3,2) from dual;--返回结果为'll'
select substr('Hello World',-7,4) from dual;--返回结果为'o Wo'

abs(value)

例子:绝对值
select abs(-10) from dual;--返回结果为10

floor(value)

例子:返回小于等于value的最大整数
select floor(2.3) from dual; --返回结果为2

trunc(value,n)

例子:对value进行截断,如果n>0,保留n位小数;n<0,则保留-n位整数位;n=0,则去掉小数部分
select trunc(555.666) from dual; --返回结果为555,不加n时默认去掉小数部分
select trunc(555.666,2) from dual;--返回结果为555.66
select trunc(555.666,-2) from dual;--返回结果为500

round(value,n)

例子:对value进行四舍五入,保存小数点右侧的n位。如果n省略的话,相当于n=0的情况
select round(555.666) from dual;--返回结果为556,不加n时默认去掉小数部分
select round(555.666,2) from dual;--返回结果为555.67
select round(555.666,-2) from dual;--返回结果为600

to_char(x[,format])

例子:将x转化为字符串。 format为转换的格式,可以为数字格式或日期格式
select to_char('12345.67') from dual; --返回结果为12345.67
select to_char('12345.67','99,999.99') from dual; --返回结果为12,345.67
SELECT to_char(SYSDATE,'YYYY-MM-DD') FROM DUAL  --2021-03-21
select to_char(sysdate,'Q') from dual;--1  Q为季度
 select to_char(sysdate,'ww') from dual; --ww 当年第几周
 select to_char(sysdate,'w') from dual;--w 本月第几周 
  select to_char(sysdate,'DDD') from dual; --DDD 当年第几天,一月一日为001

to_number(x [, format])

例子:将x转换为数字。可以指定format格式
select to_number('970.13') + 25.5 from dual;  --995.63
select to_number('-$12,345.67', '$99,999.99') from dual;---12345.67

cast(x as type)

例子:将x转换为指定的兼容的数据库类型
select cast(12345.67 as varchar2(10)),cast('05-7月-07' as date), 
cast(12345.678 as number(10,2)) from dual;  --	12345.67	05-7月 -07	12345.68

to_date(x [,format])

例子:将x字符串转换为日期
select to_date('2012-3-15','YYYY-MM-DD') from dual --15-3月 -12

add_months(val1,val2)

例子:当前日期val1后推val2个月
select add_months(sysdate,2) from dual; --21-5月 -21

**

decode(条件,值1,返回值1,值2,返回值2,…值n,返回值n,缺省值)

IF 条件=1 THEN
    RETURN(翻译值1)
ELSIF 条件=2 THEN
    RETURN(翻译值2)
    ......
ELSIF 条件=值n THEN
    RETURN(翻译值n)
ELSE
    RETURN(缺省值)
END IF
SELECT decode(6,1,1,3,3,4,4,6) FROM dual --6

**

sign()

例子:根据某个值是0、正数还是负数,分别返回01-1
select id,sign(id-2) from dual;  -x

**

lpad()

例子:在字段id前边补字段0 长度为2
select lpad(id,2,0) from t_decode;-x

**

instr()

例子:字符查找函数
select * from tableName a where name like '%helloworld%';
select * from tableName a where instr(name,'helloworld')>0; 

**

case when then else end

例子:判断字段语句  || 实现行列转换
SELECT CASE 2 WHEN 2 THEN 222 ELSE  00 end FROM dual  --222
同等于
select 
   decode(2,2,222,00) id_1,
   decode(3,3,333,00) id_2,
   decode(4,4,444,00) id_3 
from dual;--222	333	444

**

case when then else end

例子:判断字段语句  || 实现行列转换
SELECT CASE 2 WHEN 2 THEN 222 ELSE  00 end FROM dual  --222
同等于
select 
   decode(2,2,222,00) id_1,
   decode(3,3,333,00) id_2,
   decode(4,4,444,00) id_3 
from dual;--222	333	444

WMSYS.WM_CONCAT||LISTAGG

例子:clob类型  || 实现行列转换多行数据逗号拼接到一个字段中,第一个在10G版本中出现,是以字段类型返回,11G中clob类型返回。第二个在11G版本对分组后的数据按照一定的排序进行字符串连接中才有
SELECT CASE 2 WHEN 2 THEN 222 ELSE  00 end FROM dual  --222
同等于
select 
   decode(2,2,222,00) id_1,
   decode(3,3,333,00) id_2,
   decode(4,4,444,00) id_3 
from dual;--222	333	444

case when then else end

例子:判断字段语句  || 实现行列转换
SELECT CASE 2 WHEN 2 THEN 222 ELSE  00 end FROM dual  --222
同等于
select 
   decode(2,2,222,00) id_1,
   decode(3,3,333,00) id_2,
   decode(4,4,444,00) id_3 
from dual;--222	333	444

START WITH …CONNECT BY CONDITION2 [PRIOR expr = expr]

例子:递归算法
自顶向下:
select empno, mgr, level as lv
from scott.emp a
start with mgr is null
connect by (prior empno) = mgr
order by level;
自下向上:
select empno, mgr, level as lv
from scott.emp a
start with empno = 7876
connect by (prior mgr ) = empno
order by level;

start with: 子句为可选项,用来标识哪行作为查找树型结构的第一行(即根节点,可指定多个根节点)。若该子句被省略,则表示所有满足查询条件的行作为根节点。
connect by : CONDITION2 [PRIOR expr = expr] : 指定层次结构中父节点与子节点之之间的关系。
PRIOR :PRIOR置于运算符前后的位置,决定着查询时的检索顺序。

猜你喜欢

转载自blog.csdn.net/YHM_MM/article/details/115044549