Oracle学习—函数

来源:互联网 发布:童瑶事件 知乎 编辑:程序博客网 时间:2024/04/27 19:36


--------------常用的函数---------
1 initcap字符函数:对字符串进行操作 ,将单个的字符或者字符串的首字母转换成大写
select initcap(c_name) from tb_class;

2 instr(字段,查找的字段):查找第二个参数在第一个参数中第一次出现的位置,如果不存在 返回0
select c_name,instr(c_name,'s') from tb_class;

3 length:返回字符串的长度
select length(c_name) from tb_class;

4 Lower:将字符串全部转换为小写 upper:将字符串全部转换为大写
select upper(c_name) from tb_class;

5 lpad(str1,len,str2):将str2填充在str1的左边 总长度达到len为止
select lpad(c_name,12,'我说算你狠') from tb_class;

6 replace(str1,str2,str3):str3替换str1中的str2字符串
select replace(c_name,'美年达','主宰') from tb_class;

---------------------------
--数值函数
create table tb_count
(
id number primary key,
num1 number not null,
num2 number not null,
c_date date,
time timestamp
);

select * from tb_count;
insert into tb_count(id,num1,num2) values(1,23,2);

1 取最近的整数(向上取值)
select ceil(13.1) from tb_count;

2 取最近的整数(向下取值)
select floor(12.8) from tb_count;

3 表示的是2的3次方的值
select power(2,3) from tb_count;

4 返回该值得平方根
select sqrt(9) from tb_count;

5 整数返回1 负数返回-1 0返回0
select sign(-2) from tb_count;

6 trunc:有两个参数 第二个参数决定小数点的位数 从整数后的第一个小数点开始取
select trunc(12.234433423,4) from tb_count;

7 round:四舍五入
select round(12.323432) from tb_count;

8 exp(n):求N的指数 后面的参数是指小数点的位数
select round(exp(2),3) from tb_count;

9 mod(被除数 除数):取得是余数 13%3=1
select mod(13,3) from tb_count;

10 ln(n):自然对数 必须大于0
select ln(exp(22)) from tb_count;

11 log(m,n):以M为底 N的对数
select log(10,100) from tb_count;

12 vsize(m):返回M所占空间的大小
select vsize(1010) from tb_count;

13 greatest:返回一组数里面最大的值
select greatest(10,11,233,56,222) from tb_count;

14 least:返回一组数值中最小的值
select least(num1,num2) from tb_count;

-----------------------------------------------
--日期函数
insert into tb_count(id,num1,num2,c_date) values(2,888,666,sysdate);
select * from tb_count;

1 add_months:在给定的时间上添加月份
select add_months(c_date,2) from tb_count;

2 last_day:返回给定日期所在月份的最后一天的日期
select last_day(c_date) from tb_count;

3 next_day(x,y):用于计算x时间后第一个星期y的时间
select next_day(c_date,'星期二') from tb_count;

4 months_between(x,y):用于计算x和y之间有几个月。如果x在日历中比y早,那么返回一个负数
select months_between(c_date,to_date('1993-8-24','yyyy-mm-dd')) from tb_count;

5 trunc:截取
select trunc(to_date('12:13:14 11-11-1990','HH:MI:SS MM-DD-YYYY'),'year') from tb_count;

6 to_char(date,str):将date按照str的格式转换字符串类型
select to_char(sysdate,'mm-dd-yyyy') from tb_count;

7 to_date(str1,str2):将str1按照str2的格式转换成日期类型
  to_number(str):将字符串转换成数值类型
  to_timestamp(str1,str2):将str1按照str2的格式转换成timestamp类型
insert into tb_count(id,num1,num2,c_date,time) values(3,to_number('100'),to_number('101'),to_date('2013-11-5','yyyy-mm-dd'),to_timestamp('2013-11-11','yyyy-mm-dd HH:MI:SS'))