oracle常用函数

来源:互联网 发布:portraiture mac 安装 编辑:程序博客网 时间:2024/06/05 22:37

1.字符连接:Concat和||

Concat连接两个字符,||可连接多个字符

 

concat( string1, string2 )

select CONCAT(menu_id ,',') from rh_collection where id ='31500123'

 

select menu_id ||',Str1'||’,Str2’from rh_collection where id ='31500123'

 

 

查询出 在其后追加 值sql

update rh_collectionset menu_id=(

selectCONCAT(menu_id ,',') from rh_collection where id ='31500123'

)

where id='31500123'

 

2.字符替换

replace'将要更改的字符串','被替换掉的字符串','替换字符串'

 

3.时间转换

Sysdate  默认年/月/日 时:分:秒  24小时制

To_char(str,’yyyy-MM-dd hh24:mi:ss’) 24小时制

To_char(str,’yyyy-MM-dd hh:mi:ss’)   非24小时制

Str为时间类型,如是字符串可通过to_date(‘20151112232311’,’yyyy-MM-dd hh:mi:ss’)


4.to_number转换为为数值型

   NVL函数是一个空值转换函数

   nvl(expr1,expr2)

   如果 expr1 是 null 值,则 nvl 函数返回 expr2 ,否则就返回 expr1 。 


 生成主键 从M1000开始

SELECT  'M'|| NVL(( MAX(TO_NUMBER(SUBSTR(F_MAJOR_INFO_NUM,2,LENGTH(F_MAJOR_INFO_NUM))))+1),1000) F_MAJOR_INFO_NUM  FROM  TG_MAJOR_INFO


5.sign()函数

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

       

6.decode()函数

    

该函数的含义如下:
IF 条件=值1 THEN
    RETURN(翻译值1)
ELSIF 条件=值2 THEN
    RETURN(翻译值2)
    ......
ELSIF 条件=值n THEN
    RETURN(翻译值n)
ELSE
    RETURN(缺省值)
END IF

select id, decode(sign(score-85),1,'优秀',0,'优秀',-1, 
decode(sign(score-70),1,'良好',0,'良好',-1, 
decode(sign(score-60),1,'及格',0,'及格',-1,'不及格'))) 
from student;

    

0 0