1.字符函数
lower、upper、initcap、substr、instr、concat、length等
(涉及到的表已放在本节末尾)
lower:把字符转换为小写
select lower('HELLO') from dual;
select lower('hEllo') from dual;
结果:均为hello
select * from emp where lower(name)='dop';
select * from emp where name='dop';
--查找出emp表中名字为‘dop’的员工
select lower(name) from emp;
--把emp表的name列的值全转换为小写
upper:把字符转换为大写
select upper('hello') from dual;
select upper('hEllo') from dual;
结果:均为HELLO
select * from emp where upper(name)='ZIV';
select * from emp where name='ZIV';
--查找emp表中名字为ZIV的员工
select upper(name) from emp;
--把emp表的name列的值全转换为大写
select upper(substr('helloworld',0,5)) from dual;
--把substr的结果转换为大写
initcap:把字符首字母变为大写
select initcap('hello') from dual;
--结果为:Hello
select initcap(name) from emp;
--把name中的值的首字母都变为大写
select * from emp where initcap(name) ='Dop';
--相当于查找出名字为dop的员工
select * from emp where initcap('dop') ='Dop';
--把emp表的信息全展示出来
--原因:initcap('dop')的值为Dop,即initcap('dop') ='Dop',也即Dop=Dop,为真。
substr()字符截取函数
格式1:substr('原字符',开始截取的起始位置,截取的长度)
格式2:substr('原字符',开始截取位置) 从开始截取位置截取所有的字符
select substr('helloworld',2,5) from dual;
结果为:ellow
select substr('helloworld',5,2) from dual;
结果为:ow
select substr('helloworld',5) from dual;
结果为:oworld
注意:select substr('helloworld',1,5) from dual;
select substr('helloworld',0,5) from dual;
结果:均为hello
注意:当字符截取的起始位置为0或1时,都是从第一位开始截取
instr()字符查找函数
格式1:instr(源字符串,目标字符串);
格式2:instr(源字符串,目标字符串,查找的起始位置,匹配序号)
在源字符串中查找目标字符,并返回目标字符所在的位置
select instr('helloworldword','world') from dual;
结果:6
select instr('helloworldword','world',1,1) from dual;
结果:6
在helloworldworld字符串查找字符world,并返回world出现第1次的位置
select instr('helloworldword','world',1,2) from dual;
结果:11
在helloworldworld字符串查找字符world,并返回world出现第2次的位置
concat()字符拼接函数:把字符拼接在一起
select concat('he','llo') from dual;
结果:hello
length:获取字符串的长度
select length('helloworld') from dual;
结果:10
select * from emp where length(name)>4;
结果:显示了Linda的所有信息
replace():替换函数
select replace('helloworld','world',' Linda') from dual;(注意:Linda前加了一个空格)
--把字符串helloworld中的world替换为 Linda
--结果:hello Linda
select length(replace('helloworld','world',' Linda')) from dual;
--结果:11
涉及到的表:
create table emp(id number(20),name varchar2(40),entry_time date);
insert into emp values(1,'Linda',sysdate);
insert into emp values(2,'dop',to_date('2022/7/25','yyyy/mm/dd'));
insert into emp values(3,'ZIV',to_date('2022/9/28','yyyy/mm/dd'));