Oracle初级测试题四--单行函数
Oracle初级测试题四--单行函数
注意
本博文仅供学术研究和交流参考,严禁将其用于商业用途。如因违规使用产生的任何法律问题,使用者需自行负责。
--练习1
--1.写一个查询,用首字母大写,其它字母小写显示雇员的 ename, --显示名字的长度,并给每列一个适当的标签,条件是满足所有雇员名字的开始字母是J、A 或 M 的雇员, --并对查询结果按雇员的ename升序排序。(提示:使用initcap、length、substr)
select initcap(ename) as 首字母大写,
length(ename) as 名字长度,
substr(ename,0,1),
substr(ename,1,1),
substr(ename,2,1)
from emp
where substr(ename,0,1) in('J','A','m')
order by ename asc;

--练习2
--1.查询员工姓名中中包含大写或小写字母A的员工姓名。
select ename,
instr(ename,'a'),
instr(ename,'A')
from emp
where instr(ename,'a') <> 0 or instr(ename,'A') <> 0;

--2.查询部门编号为10或20,入职日期在81年5月1日之后,并且姓名中包含大写字母A的员工姓名,员工姓名长度(提示,要求使用INSTR函数,不能使用like进行判断)
select ename,
length(ename),
hiredate,
deptno,
instr(ename,'A')
from emp
where deptno in(10,20) and
hiredate > '01-5月-81' and
instr(ename,'A') <> 0;

--3.查询每个职工的编号,姓名,工资 --要求将查询到的数据按照一定的格式合并成一个字符串. --前10位:编号,不足部分用填充,左对齐 --中间10位:姓名,不足部分用填充,左对齐 --后10位:工资,不足部分用*填充,右对齐
select deptno,
ename,
sal,
lpad(deptno,10,'*'),
lpad(ename,10,'*'),
rpad(sal,10,'*')
from emp;

--练习3
--1.写一个查询,分别计算100.456 四舍五入到小数点后第2位,第1位,整数位的值。
select round(100.456,2),
round(100.456,1),
round(100.456,0)
from dual;

--2.写一个查询,分别计算100.456 从小数点后第2位,第1位,整数位截断的值。
select trunc(100.456,2),
trunc(100.456,1),
trunc(100.456,0)
from dual;

--练习4
--1.查询每个员工截止到现在一共入职多少天?
select ename,trunc(sysdate - hiredate) as 到现在入职的天数
from emp;


口决:
RR: 大小进一 小大退一
YY: 与RR相反
大大小小均不变(备注大指当前年份,小为指定年份)
--2.当前日期为2015年,指定日期格式为DD-MON-RR,指定日期为01-1月-01,该日期实际所代表的日期为?
2001-01-01
--3.当前日期为2015年,指定日期格式为DD-MON-RR,指定日期为01-1月-95,该日期实际所代表的日期为?
1995-01-01
--4.当前日期为1998年,指定日期格式为DD-MON-RR,指定日期为01-1月-01,该日期实际所代表的日期为?
2001-01-01
--5.当前日期为1998年,指定日期格式为DD-MON-RR,指定日期为01-1月-95,该日期实际所代表的日期为?
1995-01-01
--6.当前日期为2015年,指定日期格式为DD-MON-YY,指定日期为01-1月-01,该日期实际所代表的日期为?
2001-01-01
--7.当前日期为1998年,指定日期格式为DD-MON-YY,指定日期为01-1月-95,该日期实际所代表的日期为?
1995-01-01
--练习5
串一个知识点。。查询所有用户表
select * from user_tables;

--1.查询服务器当前时间
select sysdate
from emp;

--2.查询部门10,20的员工截止到2000年1月1日,工作了多少个月,入职的月份。(提示:使用months_between,extract)
select hiredate,
trunc(months_between('01-1月-00',hiredate)) 工作月数,
extract(month from hiredate)
from emp
where deptno in(10,20);

--3.如果员工试用期6个月,查询职位不是MANAGER的员工姓名,入职日期,转正日期,入职日期后的第一个星期一,入职当月的最后一天日期。(提示:使用add_months,next_day,last_day)
select hiredate as 入职日期,
add_months(hiredate,6) as 转正日期,
next_day(hiredate,'星期一') as 入职后的第一个星期一,
last_day(hiredate) as 入职当月的最后一天日期
from emp
where job not in 'MANAGER';

--练习6
--1.显示服务器系统当前时间,格式为2007-10-12 17:11:11(提示:使用to_char函数)
select sysdate,
to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')
from emp;

--2.显示ename、hiredate 和 雇员开始工作日是星期几,列标签DAY(提示:使用to_char函数)
select ename,hiredate,to_char(hiredate,'DAY') as DAY
from emp;

--3.查询员工姓名,工资,格式化的工资(¥999,999.99) (提示:使用to_char函数)
备注:此处只能转换成$不能¥且此处的第二参数为固定'$99,999'少写会报错
select ename,sal,to_char(sal,'$99,999')
from emp;

--4.把字符串2015-3月-18 13:13:13 转换成日期格式,并计算和系统当前时间间隔多少天。 (提示:使用to_date函数)
select to_date('2015-3月-18 13:13:13','yyyy-mm"月"-dd hh24:mi:ss') as 日期,
trunc(sysdate-to_date('2015-3月-18 13:13:13','yyyy-mm"月"-dd hh24:mi:ss')) as 相差的天数
from emp;

--课后作业
--1.计算2000年1月1日到现在有多少月,多少周(四舍五入)。
select round(months_between(sysdate,'01-1月-00')),
round(months_between(sysdate,'01-1月-00')*30/7)
from emp;

--2.查询员工ENAME的第三个字母是A的员工的信息(使用2个函数)。
select ename,instr(ename,'A',3,1)
from emp
where instr(ename,'A',3,1) = 3;--从第三个位置开始第一次出现的位置

select ename,substr(ename,3,1)
from emp
where substr(ename,3,1) in 'A';--从第三个位置开始截取一个字符串

--3.使用trim函数将字符串‘hello’、‘ Hello ’、‘bllb’、‘ hello ’分别处理得到下列字符串ello、Hello、ll、hello。
select trim(leading 'h' from 'hello'),
trim(both ' ' from ' Hello '),
trim(both 'b' from 'bllb'),
trim(trailing ' ' from 'hello ')
from dual;

--4.将员工工资按如下格式显示:123,234.00 RMB 。
select sal,to_char(sal,'$99,999')
from emp;--这题我只能写成这样。。。若有人解出来。。请务必告知呀

--5.查询员工的姓名及其经理编号,要求对于没有经理的显示“No Manager”字符串。
select ename,mgr,nvl(to_char(mgr),'No Manager') from emp; -- 据说 nvl()里两参数必须是同一类型。。否则。。错死你 o
--6.将员工的参加工作日期按如下格式显示:月份/年份。
select hiredate,
extract(month from hiredate) || '/' || extract(year from hiredate) as "月份/年份"
from emp;
select hiredate,to_char(hiredate,'mm/yyyy') as "月份/年份"
from emp;

--7.在员工表中查询出员工的工资,并计算应交税款:如果工资小于1000,税率为0,如果工资大于等于1000并小于2000,税率为10%,如果工资大于等于2000并小于3000,税率为15%,如果工资大于等于3000,税率为20%。
select ename,sal,
case
when sal < 1000 then 0
when sal >=1000 and sal < 2000 then (sal-1000)*0.1
when sal >=2000 and sal <3000 then 1000 * 0.1 + (sal - 2000) * 0.15
when sal > 3000 then 1000 * 0.1 + 1000 * 0.15 + (sal - 3000) * 0.2
else 0
end as 税款
from emp;

--8.创建一个查询显示所有雇员的 ename和 sal。格式化sal为 15 个字符长度,用 $ 左填充,列标签 SALARY。
select ename,sal,lpad(sal,15,'$') as SALARY
from emp;

分割线
相关信息
以上就是我关于 Oracle初级测试题四--单行函数 知识点的整理与总结的全部内容,希望对你有帮助。。。。。。。