当前位置: 代码迷 >> 综合 >> oracle的一些常用函数。。。。。。。。。。。。
  详细解决方案

oracle的一些常用函数。。。。。。。。。。。。

热度:39   发布时间:2024-01-22 03:37:57.0
字符串函数
求总函数sum(col)   select sum(22) from dual
排除查询结果中的相同项distinct     
两列整合col||col   多个列也可以 
select concat('Hello',' world') from dual; 连接字符串,但没有||好用select concat('Hello','world') from dual; 
升降序排列 order by col    默认是升序,降序的话后面加 desc 
select upper('coolszy') from dual; 将小写字母转换成大写,dual 为一虚表 
select lower('KUKA') from dual; 将大写字母转换成小写  
select initcap('kuka') from dual; 将首字母大写    
select concat('Hello',' world') from dual; 连接字符串,但没有||好用select concat('Hello','world') from dual;   
select substr('hello',1,3) from dual; 截取字符串   
select length('hello') from dual; 求字符串长度   
select replace('hello','l','x') from dual; 替换字符串    
select substr('hello',-3,3) from dual; 截取后三位   
求出某列值的长度  length(col)
select round(789.536) from dual; 四舍五入,舍去小数   
select round(789.536,2) from dual; 保留两位小数   
select round(789.536,-1) from dual; 对整数进行四舍五入   
select trunc(789.536) from dual; 舍去小数,但不进位   
select trunc(789.536,2) from dual;   
select trunc(789.536,-2) from dual;   
select mod(10,3) from dual; 返回10%3的结果
日期函数
--查询一段时间内的数据   
data   between 'data 1' and  'data2'
--格式化时间  to_char(data1,"格式化样式")
--获取日期后第一个星期X的日期 next_day(date,'星期X')       这个函数真难,查了半天才发现有这个,但真的确实是最好用的,省了好多时间计算
--取两个日期中的月份总数 months_between(data1-data2) 因为有小数,可以用round取整
select months_between(sysdate,'16-6月 -08') from dual; 返回之间的月数  
--日期加一年add_months(data,12)    本来是想用data+365的,但一想有闰年平年,有时会有366天,所以傻了。只能用这个函数
select add_months(sysdate,4) from dual; --在日期上加上月数  
select sysdate from dual; --返回当前日期   
select next_day(sysdate,'星期一') from dual; --求下一个星期一   
select last_day(sysdate) from dual; --求本月的最后一天 
转换函数
select to_char(sysdate,'yyyy') year,to_char(sysdate,'mm'),to_char(sysdate,'dd') from dual;   
select to_char(sysdate,'yyyy-mm-dd') from dual;   
select to_char(sysdate,'fmyyyy-mm-dd') from dual; --取消月 日 前面的0   
select to_char('20394','99,999') from dual; --分割钱 9表示格式   
select to_char('2034','L99,999') from dual; --加上钱币符号  
select to_char(sysdate,'D')from dual;  --一周中的星期几
select to_char(sysdate,'DAY')from dual; --星期几的中文名字
select to_char(sysdate,'DD')from dual; --月中的第几天
select to_char(sysdate,'DDD')from dual; --年中的第几天 
select to_char(sysdate,'IW')from dual; --标准的年中的第几周 
select to_char(sysdate,'IYYY')from dual; --标准的四位年份
select to_char(sysdate,'YYYY')from dual; --四位年份
select to_char(sysdate,'YYY')from dual; --年份的最后三位
select to_char(sysdate,'YY')from dual; --年份的最后二位
select to_char(sysdate,'Y')from dual; --年份的最后一位
select to_char(sysdate,'YYYY-MM-DD HH12')from dual; --按12小时计
select to_char(sysdate,'YYYY-MM-DD HH24')from dual; --按24小时计
select to_char(sysdate,'HH24:MI:SS')from dual; --小时,分,秒
select to_char(sysdate,'w')from dual;  --该月的第几个星期 
select to_char(sysdate,'ww')from dual; --年中的第几个星期
select sysdate,sysdate - interval '7' MINUTE from dual  --当前时间减去7分钟的时间 
select sysdate - interval '7' hour from dual  --当前时间减去7小时的时间 
select sysdate - interval '7' day from dual  --当前时间减去7天的时间
select sysdate,sysdate - interval '7' month from dual  --当前时间减去7月的时间
select sysdate,sysdate - interval '7' year from dual  --当前时间减去7年的时间
select sysdate,sysdate - 8 *interval '2' hour from dual  --时间间隔乘以一个数字
select to_char(current_timestamp(5),'DD-MON-YYYY HH24:MI:SSxFF') from dual; --返回当前时间 年月日小时分秒毫秒 
select to_char(current_timestamp(9),'MI:SSxFF') from dual; --返回当前 时间的秒毫秒,可以指定秒后面的精度(最大=9) 
select to_number('123')*to_number('2') from dual;   
select to_date('1988-07-04','yyyy-mm-dd') from dual;    
应用函数
select nvl(null,0) from dual; --如果为null,则用0代替   
判断语句 decode    decode(col,<if this value>,<return thiis value>) (可选col2)     col是判断列 return this value是返回的值,但不一定返回给该列,可以返回col2列
判断语句case case when <判断语句> then value when <判断语句> then value else value end
select sid,serial#,username,DECODE(command,0,'None',2,'Insert',3,'Select',6,'Update',7,'Delete',8,'Drop','Other') cmmand
from v$session where username is not null;  
正则表达式的函数
1,REGEXP_LIKE :与 LIKE的功能相似 
2,REGEXP_INSTR :与INSTR的功能相似 
3,REGEXP_SUBSTR :与SUBSTR 的功能相似 
4,REGEXP_REPLACE :与REPLACE的功能相似 
5,REGEXP_COUNT     :统计字符串出现次数
事务处理 
1:commit;  提交事务  
2:rollback; 回滚事务  

3:select rownum from table; 在没一列前面显示行号  
4:drop table 表名 cascade constraint   
on delete casecade 当父表中的内容被删除后,子表中的内容也被删除  

5:desc表名 显示表的结构  
6:create user [username] identified by [password] 创建新的用户  
7:grant 权限1、权限2...to 用户  给创建用户权限  
ex:grant create session to [username] 此时只能连接到数据库  
grant connect,resource to [username] 此时权限能满足要求  
8:alter user [username] identified by [password] 修改用户密码  
9:alter user [username] password expired 下次登录时提示修改密码  
10:alter user [username] account lock  锁住用户  
11:alter user [username] account unlock  解锁锁用户  
12:grant select,delete on scott.emp to [username] 把scott下emp表的两个权限给用户  
13:revoke select ,delete on scott.emo from [username] 回收权限