select sysdate from dual;
select length('aa') from dual;
select ename,length(ename) from emp;
--查询姓名有6个字符的员工信息
select * from emp where length(ename)=6;
select concat('aa','bb') from dual;
||:字符串连接符
select 'aa'||'bb' from dual;
select concat(concat('aa','bb'),'cc') from dual;
select 'aa'||'bb'||'cc' from dual;
select 'drop table '||table_name||':' from user_tables;
注意:字符串要用单引号括起来,在字符串(单引号中)中使用两个连着的单引号,这时第一个单引号是一个转义符号
select '''' from dual;
select ',''' from dual;
select 'insert into dept values( '||deptno||','''||dname||
''','''||loc||''');' from dept;
select chr(39) from dual;
select chr(85) from dual;
select chr(97) from dual;
select 'insert into dept values('||deptno||','||chr(39)||dname||chr(39)
||','||chr(39)||loc||chr(39)||');' from dept;
表示从字符串str的 index 位置开始截取 len 个长度的字符
select substr('abcdefg',2,2) from dual;
select substr('abcdefg',2) from dual;
select substr('abcdefg',2,6) from dual;
select substr('abcdefg',2,8) from dual;
select ' abcd ' from dual;
select trim(' abcd ') from dual;
select trim('$' from '$abcd$$') from dual;
ltrim(str[,‘s’]):去除字符串左边的空格或者字符 s
rtrim(str[,‘s’]):去除字符串右边的空格或字符 s
select ltrim(' abcd ') from dual;
select rtrim(' abcd ') from dual;
select ltrim('$abcd$','$') from dual;
select rtrim('$abcd$','$') from dual;
表示将字符串 str 中的 s 替换成 d
select replace('abcd1qewr1qerazdf','qe','QE') from dual;
表示在字符串的左边填充空格或者 s 让字符串的长度到 len 的长度。
select lpad('aa',4) from dual;
select lpad('aa',4,'b') from dual;
select lpad('abcd',2,'6') from dual;
select rpad('abcd',2,'6') from dual;
select initcap('hello world') from dual;
select initcap('helloworld') from dual;
select lower(ename) from emp;
select upper(ename) from emp;
在字符串 str 中从 n1 的位置开始查询第 n2 次出现字符 s 的位置
--差字符串首次出现 j 得位置
select instr('asdoqwerjldajsfoqjwelfjaoidsfj','j') from dual;
--从字符串的第10个字符开始查找第一次出现 j 的位置
select instr('asdoqwerjldajsfoqjwelfjaoidsfj','j',10) from dual;
--从字符串的第10个字符开始查找第三次出现 j 的位置
select instr('asdoqwerjldajsfoqjwelfjaoidsfj','j',10,3) from dual;
--从字符串的倒数第3个字符开始反向查找第三次出现 j 的位置
select instr('asdoqwerjldajsfoqjwelfjaoidsfj','j',-3,3) from dual;
select floor(3.41) from dual;
select floor(3.98) from dual;
select mod(3,2) from dual;
select round(3.134) from dual;
select round(5.76) from dual;
select round(5.68,1) from dual;
select power(2,3) from dual;
select to_number('78.1234') from dual;
yyyy:表示4位的年
MM:表示两位月
dd:表示两位的天
hh24:表示24小时制的小时
mi:表示分钟
ss:表示秒
day:表示星期
select to_date('1999-09-09 14:24:34','yyyy-MM-dd hh24-mi-ss') from dual;
select to_char(sysdate,'yyyy') from dual;
select to_char(sysdate,'hh24:mi:ss') from dual;
select to_char(sysdate,'day') from dual;
select add_months(sysdate,3) from dual;
select add_months(sysdate,-3) from dual;
日起直接加上一个整数,相当于加的天数
select sysdate+1 from dual;
select months_between(sysdate,to_date('20210120','yyyyMMdd')) from dual;
select last_day(sysdate) from dual;
select current_date from dual;
--截断数字类型,第二个参数表示精度,不会四舍五入
select trunc(3.13453,3) from dual;
--截取到年(本年的第一天)
select trunc(sysdate,'yyyy') from dual;
--截取到月(本月的第一天)
select trunc(sysdate,'MM') from dual;
select trunc(sysdate,'month') from dual;
--截取到周(本周第一天,即上周日)
select trunc(sysdate,'day') from dual;
exp:表达式
res1,res2,… 表达式的结果
如果表达式的结果和某一个 res 值相等,就会返回 res 后面对应的 value,如果表达式的结果没有一个 res 值和它匹配,它会返回 default 默认值
select decode(ssex,'男','男生','女','女生','未知') from student;
select nvl(comm,200) from emp;
需要行转列的业务逻辑通常是:将表中的一个字段分类统计后作为多个结果字段输出
如:
create table studentScores(
username varchar2(20),
subject varchar2(30),
score number
);
insert into studentScores values('张三','语文',90);
insert into studentScores values('张三','英语',80);
insert into studentScores values('张三','数学',70);
insert into studentScores values('李四','语文',60);
insert into studentScores values('李四','英语',61);
insert into studentScores values('李四','数学',62);
select * from studentScores;
select username,
sum(case when subject='语文' then score else 0 end) 语文,
sum(case when subject='英语' then score else 0 end) 英语,
sum(case when subject='数学' then score else 0 end) 数学
from studentScores group by userName;
pivot 函数是行转列的函数
--用pivot函数查出来的字段名就是’语文‘,注意这两个单引号也是名称一部分,所以这行要用双引号括起来
select username,"'语文'","'英语'","'数学'" from Studentscores
pivot(sum(score) for subject in('语文','英语','数学'));
如:
create table studentScores2(
username varchar2(20),
yuwen number,
yingyu number,
shuxue number
);
insert into studentScores2 values('张三',90,80,70);
insert into studentScores2 values('李四',60,61,62);
select * from studentScores2;
select username,'yuwen' as subject,yuwen as score from studentScores2
union all
select username,'yingyu',yingyu from studentScores2
union all
select username,'shuxue',shuxue from studentScores2;
unpivot 函数是列转行函数
select username,subject,score from studentScores2
unpivot(score for subject in(yuwen,yingyu,shuxue));