单行函数的使用

---单行函数的使用
 -------------------------------------------------------------
 --大小写函数
 --大写变小写
 select lower('HELLO WORD') from dual;
 --小写变大写
 select UPPER('hello word') from dual;
 --每个单词首字母大写
  select initcap('hELLO word') from dual;
 
 ---------------------------------------------------------
 --字符控制函数:
 --sql里面的字符下脚标从1开始,java从零开始。
 select 
 concat('he','llo'),--字符串连接
 substr('helloworld',2,6),  --截取字符串
 length('helloword') --计算字符串长度
 from dual;
 --返回w在第一个字符串中出现的位置,不存在返回0
 select instr('hello word','w') from dual; 
 --左对齐补齐
 select e.employee_id,e.salary,lpad(e.salary,10,'#') from employees e
 --右对齐补齐
  select e.employee_id,e.salary,rpad(e.salary,10,'#') from employees e
  --trim   将一个单字符,从另一个串中移除,只去首尾的
  select trim('h'from'helhlo hwordh') from dual;
  --replace 将一个字符串中的所有的字符b替换成字符m
  select replace('abcdaera','a','y') from dual;
 -------------------------------------------------
 
 --数字函数:
 --round(n,m)  返回舍入小数点右边 m 位的n的值
  select round(444.66,2),round(3444.78),round(344.34,-1) from dual;
   --trunc(n,m)  截断小数点右边 m 位的n的值
    select trunc(444.66,2),round(3444.78),round(344.34,-1) from dual;
    --mod  求余
    select mod(1100,300) from dual;
    select mod(1100,200) from dual;
    --ABS  取绝对值
    select ABS(-122) from dual;
    --power(n1,n2)  返回n1的n2次方
    select power(2,3) from dual;
    -------------------------------------------------------
   
   --日期函数:oracle中有两个东西
   日期,时间 
   sysdate()返回系统当前的日期
   select sysdate,sysdate+1,sysdate -1 from dual;
   --日期和日期之间只能做减法,现在的时间减去员工入职时间等于工作时间
  --多少天 
   select e.employee_id,e.last_name,trunc(sysdate - e.hire_date) as 工作时长 from employees e 
   --判断两个日期相差的月份数
   select months_between(sysdate,e.hire_date) from employees e
   --add_months  向指定日期中加上多少个月
   select add_months(sysdate,2) from dual;
   --last_day  本月的最后一天
   select last_day(sysdate) from dual;
   
   select round(sysdate,'year'),round(sysdate,'mm'),trunc(sysdate,'hh') from dual;

  

posted @ 2025-04-19 23:01  沧海一粟95  阅读(12)  评论(0)    收藏  举报