`

Oracle中一些常用的函数整理

 
阅读更多
select ceil(10),ceil(10.1),ceil(-10.5) from dual;



select greatest(1,2,3,-1,0) from dual;  --数字中的最大值

select greatest('one','ten') from dual;  --字符串中的最大值

select greatest(to_date('2014-01-08','yyyy-mm-dd'),to_date('2013-05-08','yyyy-mm-dd')) from dual;

select least(1,2,3,-10,0) from dual;  --数字中最小值


select trunc(sysdate,'yyyy-mm') from dual;


select ASCII('a'),ASCII('abc') from dual;

select CONCAT('Good','Morning'), 'Good' || 'Morning' from dual; --concat 相当于连接操作符(||)

select INITCAP('Good MORNING,QQ') from dual;  --首字母大写,其余字母小写


select instr('easycome,easygo','easy') from dual;
select instr('easycome,easygo','easy',1,2) from dual;

select instrb('easycome,easygo','easy') from dual;
select instrb('easycome,easygo','easy',1,2) from dual;

select length('ABC') from dual;  --返回字符串中的字符数
select lengthb('ABC') from dual;  --返回字符串中的字节数,一般情况下lengthb返回结果与length相同,使用多字节字符集时有所不同;

select lower('ADB') from dual;  --将所有字符串转换为小写
select upper('abD') from dual;  --将所有字符串转换为大写

LPAD(String1,n[,String2]) --在String1的左边添加空格,或者重复填充由String2指定的字符串,直到结果字符串达到n个字符为止;[,String2]为可选参数,默认为单个空格
select LPAD('X',3),LPAD('X',3,'Y'),LPAD('RIGHT',12,'LEFT') from dual;
select RPAD('X',3),RPAD('X',3,'Y'),RPAD('RIGHT',12,'LEFT') from dual;




select LTRIM(string1,[,string2]) from dual; --string1指定要删除的字符串,string2指定包含要被删除的字符集的字符串,该参数默认为单个空格,当函数遇到字符串中没有提供字符时,删除字符过程结束
select LTRIM(' ABC miss') from dual;
select RTRIM(' ABC miss.   ') from dual;
select LTRIM(' ****@@*@**ABC miss','*@') from dual;


select replace('123abc456d111','abc') from dual;
select replace('123abc456d111','abc','ABC') from dual;


select soundex('i') from dual;

select subStr('OneTwoThree',7) from dual;
select subStr('OneTwoThree',4,3) from dual;
select subStr('OneTwoThree',-5) from dual;


select TRANSLATE('123.45','0123456789','abcdefghij') from dual;
select TRANSLATE('123.45','0123456789.','abcdefghij') from dual;


select trim(' ABC  ') from dual;  --去左右空格
select trim(LEADING FROM ' ABC  ') from dual;  --去左边空格
select trim(TRAILING FROM ' ABC  ') from dual;  --去右边空格
select trim(BOTH FROM ' ABC  ') from dual;  --去左右空格
select trim(LEADING '$' from '$123.45$') from dual;

select lower('ABC') from dual;
select upper('abc') from dual;


select add_months(sysdate,1) from dual;
select add_months(sysdate,-1) from dual;
select add_months(date'2014-01-29',1) from dual;

select last_day(sysdate) from dual;  --当前月的最后一天

select months_between(date'2014-12-25',date'2013-12-25') from dual;


select to_date('2014-03-01','yyyy-mm-dd') - 1 from dual;

--select next_day('20-May-2000','Sun') from dual;

alter session set nls_date_format = 'yyyy-mm-dd hh24:mi';
select round(to_date('2014-12-25 16:35')) day,
       round(to_date('2014-12-25 16:35'),'hh') hour,
       round(to_date('2014-12-25 16:35'),'yyyy') year  from dual;

alter session set nls_date_format = 'yyyy-mm-dd hh24:mi';      
select trunc(to_date('2014-12-25 16:35')) day,
       trunc(to_date('2014-12-25 16:35'),'hh') hour,
       trunc(to_date('2014-12-25 16:35'),'yyyy') year  from dual;
      

--转换函数:
---------允许将一种数据类型转换为另一种数据类型:
to_char:转换日期和数字值为字符串
to_date:转换字符串为日期值
to_number:转换字符串为数字
select * from dual where rowid = CHARTOROWID('AAAAECAABAAAAgiAAA'); --将char或varchar2型值转换为rowid的值
convert(string,dest_char_set[,sourch_char_set]):--将字符串从一个字符集转换为另外一个字符集
select convert(chr(194) || chr(133) || chr(64) || chr(213) || chr(137) || chr(131) || chr(133),'US7ASCII') from dual;
select dump(hextoraw('C28540D5B983B5')) from dual;  --转换字符串中包含的十六进制数字对应于那些数字组成的raw值
select rawtohex(hextoraw('C28540D5B983B5')) from dual;  --转换raw值为十六进制数字的字符串
select rowid, rowidtochar(rowid) from dual;

--to_lob
select to_multi_byte('Be nice') from dual; --转换单字节字符为多字节等同物
select to_single_byte('Be nice') from dual; --转换多字节字符为单字节等同物
select translate('be nice' USING NCHAR_CS) from dual; --转换文本为数据库字符集或国际字符集

decode:提供嵌入if语句的能力--限制最大有255个参数

select dump(sysdate) from dual;
select empty_blob() from dual; --返回空blob的位置,可以用于初始化blob列;
select empty_clob() from dual; --返回空clob的位置,可以用于初始化clob列;

select nls_charset_decl_len(100,nls_charset_id('US7ASCII')) from dual; --在提供的字节计数的基础上,返回nchar列的定义宽度
select nls_charset_id('US7ASCII'),nls_charset_id('WE8EBCDIC37C') from dual;  --
select nls_charset_name(1),nls_charset_name(90) from dual;


select nvl(null,'Is Null') from dual;
select nvl('NOT Null','Is Null') from dual;

sys_context(namespace,attribute_name,[,length])--返回应用程序上下文命名空间中的属性值
select sys_context('USERENV','SESSION_USER') from dual;


select sys_guid() from dual;  --返回可以用作全局唯一标识符的16字节的RAW值
select UID from dual;  --返回唯一定义当前数据库用户的整数值
select user from dual; --返回当前用户,在存储过程或函数中调用时,返回存储过程或函数所有者的名字;在触发器中调用时,返回登录用户名
select userenv('INSTANCE'), userenv('ISDBA') from dual; --返回有关当前用户的信息
select vsize(sysdate) from dual;  --以字节的形式返回内部代表值的大小
select length(sysdate) from dual;
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics