单行函数

/**

  单行函数:字符串函数、数值函数、日期函数、转换函数、通用函数
**/

# 字符串函数:


length() 字符串长度
中文/符号:
  utf-8编码:一个汉字/符号,占3个字节
  gbk编码:一个汉字/符号,占2个字节
查询当前系统的编码格式: select * from nls_database_parameters;

--查询
  1.范围查询 between...and...
  2.模糊查询 like
【重点】:select * from emp where ename like '%\_%' escape '\';
not in...不能出现null

 

lower、upper、initcap(首字母大写)、

substr 字符串截取函数 :

 格式1: substr(string string, int a, int b);

 格式2:substr(string string, int a) ;

解析:

    格式1:
        1、string 需要截取的字符串
        2、a 截取字符串的开始位置(注:当a等于0或1时,都是从第一位开始截取)
        3、b 要截取的字符串的长度

select substr('Hello',2,3) from dual;  --ell


    格式2:
        1、string 需要截取的字符串
        2、a 可以理解为从第a个字符开始截取后面所有的字符串。

 

lenth() 字符长度、lengthb() 字节长度

  注意:英文/数字 占1个字节;当汉字/符号 :utf-8编码时,占用3个字节;gbk编码时,占用2个字节。

lpad('hello',10,'*')、rpad('hello',10,'*')  左/右填充

trim('X' from 'XXXXhello worldXXXX')   去除,默认是去掉两侧的空格

select trim('X' from 'XXXXhello worldXXXX') from dual;

replace() 替代函数

 

# 数值函数

round(数字,n位数):四舍五入
trunc(数字,n位数):舍尾,保留n位小数

# 日期函数

sysdate:当前时间
格式化:日期――>字符
to_char(日期,格式)

日期+/-数字(默认是天)
日期-日期(计算员工入职天数)

month_between(日期1-日期2) --计算期间有多少天?
add_month(日期,月数)

 

concat(a,b) 拼接字符串

select concat('xx','yy') from dual;


replace()替换

select replace('我爱你','','') from dual;


instr(1,2) 查找字符在字符串中第一次出现的位置 1:要搜索的字符串 2:查找的关键字

select instr('java','a') from dual;


lpad/rpad() 左右填充:

select lpad('hello',10,'*') 左 ,rpad('hello',10,'*') 右 from dual;


trim 可以去掉任何东西,默认去掉空格

select trim(' hello world ') from dual; ---hello world
select trim('X' from 'XXXXXhello worldXXXXX') from dual; ---hello world


round()四舍五入

select round(3.1415926,3) from dual;

select round(67.183,2)一,round(67.183,1)二,round(67.183,0)三,round(67.183,-1)四,round(67.183,-2)
from dual;


trunc()保留几位小数,不会四舍五入

select trunc(3.1415926,2) from dual;

 

select trunc(salary/21,2) from s_emp;
select trunc(67.183,2)一,trunc(67.183,1)二,trunc(67.183,0)三,trunc(67.183,-1)四,trunc(67.183,-2)
from dual;

# 转换函数

1.隐式转换(自动转换)
  字符<――>数字
  字符<――>日期
2.显示转换(常用)
  to_number()
  to_char()
  to_date()

--to_char(1,2) 1:被转换数据 2:字符串模板 任意类型――>字符串

select to_char(sysdate,'yyyy"年"mm"月"dd"日" hh24"时"mi"分"ss"秒"') from dual;


--L:本地货币

select to_char(100,'L9999.99') from dual;
select to_char(100,'$9999.99') from dual;
select to_char(salary,'$9999.00') from dual;


--to_date(1,2) 1:被转换数据 2:字符串模板 字符串――>日期类型

insert into s_emp(borndate) values(to_date('2018-12-16','yyyy-mm-dd')) from dual;


--to_number(1,2) 1:被转换数据 2:字符串模板 字符串――>数值型

select to_number('100')+100 from dual;

# 通用函数

-- 1.nvl/nvl2


  nvl:if
  nvl(comm,0)

  nvl2:if...else..
  nvl2(comm,x,0)

select ename,comm,nvl(comm,0),nvl2(comm,comm,0) from emp;


-- 2.nullif(a,b) :a=b,null 否则返回a

select nullif('abc','abc') from dual; --NULL
select nullif('abc','abcs') from dual; --abc

--3.coalesce:从左往后 找到第一个不为null的值

select comm,sal,coalesce(comm,sal) from emp;

--4.条件判断函数:
--decode(字段,条件1,返回值1,字段,条件2,返回值2,...最后表达式)

select ename,job,sal 涨前,decode(job,'PRESIDENT',sal+1000,'MANAGER',sal+500,sal+300) 涨后 
from emp;

--case表达式

select ename,job,sal 涨前,case job
when 'PRESIDENT' then sal+1000
when 'MANAGER' then sal+500
else sal+300 end
涨后
from emp;

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

show recyclebin;--查看回收站
purge recyclebin;--清空回收站 : drop table test02 purge;删除表并清空 

# 还原回收站:闪回技术

--处理多个数据的函数:聚合函数(组合函数)――>avg()/sum()/max()/min()/count()

select count(id) from s_emp; --id没有列控制,返回的是全部行数


select count(commission_pct) from s_emp; --奖金列有控制,返回的是有奖金的总行数

 

posted @ 2020-07-21 23:15  弹弹大魔王  阅读(22)  评论(0编辑  收藏  举报