单行函数练习

我们看一下这一章里面的练习题,我们先看txt里边的
1. 打印出 "2009年10月14日 9:25:40" 格式的当前系统的日期和时间.select to_char(sysdate, 'YYYY"年"MM"月"DD"日" HH:MI:SS')from dual	注意: 使用双引号向日期中添加字符打印出这种格式的当前系统的日期和时间,这个格式的当前日期和时间,就是说当前我们这个系统,我们讲的时候也说了,既然有日期也含有时间,那我们现在就来打印一下,select,那肯定是sysdate了,但是要显示这种格式的,特殊的一种格式类型,那这个我们应该怎么写,这个显示的格式是一个字符型的,那字符型的,sysdate是字符型的,那我就使用的叫to_char,长什么样,yyyy表示的是年,我们先写这样一个格式吧,小时是hh,冒号mi,minute,冒号ss,然后from dualselect to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual

再注意,人家这里写的是年月日,所以你得把这个改成双引号,年,月,日select to_char(sysdate,'yyyy"年"mm"月"dd"日"') from dual	

然后我们运行一下看一下结果,这就是指定的格式类型

2. 格式化数字: 1234567.89 为 1,234,567.89select to_char(1234567.89, '999,999,999.99')from dual										   这里是一个带逗号,char类型的,to_char,将一个number类型,转换成一个char类型3. 字符串转为数字时1). 若字符串中没有特殊字符, 可以进行隐式转换:select '1234567.89' + 100from dual2). 若字符串中有特殊字符, 例如 '1,234,567.89', 则无法进行隐式转换, 需要使用 to_number() 来完成select to_number('1,234,567.89', '999,999,999.99') + 100from dual															  字符串转成数字的时候,弱字符串中没有特殊的字符,我们可以直接的进行一个加减的运算,相当于进行了一个隐式的转换,他就是一个number类型的,如果他有特殊的字符了,像这个一样有逗号,那就无法进行隐式的转换to_number,按照这个转换成一个number类型,再加上1004. 对于把日期作为查询条件的查询, 一般都使用 to_date() 把一个字符串转为日期, 这样可以不必关注日期格式select last_name, hire_datefrom employeeswhere hire_date = to_date('1998-5-23', 'yyyy-mm-dd')
--	where to_char(hire_date,'yyyy-mm-dd') = '1998-5-23'把日期作为查询条件进行查询的时候,做日期查询的时候,要么格式非常严格,错一点都不行,要么我们这样来写,真正使用的时候都是使用的转换函数,转换函数用哪个都行,你既可以把char类型转成date型,也可以将hire_date作为date型转成char类型,都行5. 转换函数: to_char(), to_number(), to_date()我们讲转换函数就这三个,我们讲的一个重点6. 查询每个月倒数第 2 天入职的员工的信息. select last_name, hire_datefrom employeeswhere hire_date = last_day(hire_date) - 1查询每个月倒数第二天来的,last_day减1呗,然后这里面放的是date型7. 计算公司员工的年薪--错误写法: 因为空值计算的结果还是空值select last_name, salary * 12 * (1 + commission_pct) year_salfrom employees--正确写法select last_name, salary * 12 * (1 + nvl(commission_pct, 0)) year_salfrom employees然后计算公司员工的年薪,这里用的是通用函数,这里写的是错的,凡是他null参与的运算值是null,我们得使用nvl,让他为null的时候变成08. 查询部门号为 10, 20, 30 的员工信息, 若部门号为 10, 则打印其工资的 1.1 倍, 20 号部门, 则打印其工资的 1.2 倍, 30 号部门打印其工资的 1.3 倍数--使用 case-when-then-else-endselect last_name, department_id, salary, case department_id when 10  then salary * 1.1when 20  then salary * 1.2when 30  then salary * 1.3end new_salfrom employeeswhere department_id in (10, 20, 30)--使用 decodeselect last_name, department_id, salary, decode(department_id, 10, salary * 1.1,20, salary * 1.2,30, salary * 1.3) new_salfrom employeeswhere department_id in (10, 20, 30)我们讲的时候讲的一道例题,也是我们讲单行函数里面的另一个重点,什么重点,一个是条件表达式,一个是转换函数,大家需要注意,那我们这一节,这一章的内容就到这,然后我们看一下word版的练习
1.	显示系统时间(注:日期+时间)
a)	select to_char(sysdate,'yyyy-mm-dd hh:mi:ss')
b)	from dual第一个显示系统的时间,转成char类型的sysdateselect to_char(sysdate,'yyyy:mi:ss') from dual我就不写年月日这三个字了

我们这里应该用字符标起来

2.	查询员工号,姓名,工资,以及工资提高百分之20%后的结果(new salary)
a)	select employee_id,last_name,salary,salary*1.2 "new salary"
b)	from employeesselect employee_id,last_name,salary,以及工资提高百分之二十的结果,那就是salary乘以1.2呗,起个名字,当你的新名字中间有空格的时候,一定要使用双引号的,new salary,from employeesselect employee_id,last_name,salary,salary*1.1 "new salary" from employees

3.	将员工的姓名按首字母排序,并写出姓名的长度(length)
a)	select last_name,length(last_name)
b)	from employees
c)	order by last_name ascselect,来一个last_name,姓名的长度,那就length(),这是一个函数,length(last_name),按首字母排序,加上一个order by,last_name,你写上asc也行,不写默认的也是ascselect last_name,length(last_name) from employees order by last_name asc

4.	查询各员工的姓名,并显示出各员工在公司工作的月份数(worked_month)。
a)	select last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month
b)	from employees查询各员工的姓名,并显示各员工在公司工作的月份数,工作多少个月select last_name,hire_date,工作的月份数,那就判断一下当前的日期,叫系统日期,跟hire_date之间,相差的时间数,工作的月份,months_between,sysdate,我们需要把sysdate写前面,hire_date,这是我们算出来的工作月份数,起个别名,workded_month,这个时候我的月份数,是由好多位的小数点,如果你想就去保留一位,你可以在外面再包一层,round也行,逗号,保留一位小数,这样写,起个别名select last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month from employees

这是他的雇佣日期,这是他来公司一共工作多少天,相减如果有小数的话,我们接着往下看5.	查询员工的姓名,以及在公司工作的月份数(worked_month),并按月份数降序排列
a)	Select last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month
b)	from employees
c)	order by workded_month desc查询员工的姓名,以及在公司的工作月份数,跟那个差不多,并按月份数降序排列,那跟这个题目变化不大,只需要在后面加上一个order by,按照一个列的别名进行排序,他的降序排,通过它descselect last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month from employees order by workded_month desc

这样写就行了,最少165.6个月

select last_name || ' earns '|| to_char(salary,'$999999')||' monthly,but wants '||to_char(3*salary,'$999999') "Dream Salary"
from employees做一个查询,别名Dream Salary,多少钱每个月,大家这里需要一点,多了一个叫$符,$符怎么处理,我们来操作一下加$符,本身是number类型的,加上$符变成char类型的了,select last_name,earns多少钱,to_char吧,salary,加上一个$符,salary他这里也没有加逗号,没加的话直接来就行,那你就写几个9,我这里写了6个9,把它转换成一个char类型的,monthly,but wants,这个呢接着把它拿过来,3倍呗,3乘以这个,起了别名叫Dream Salaryselect last_name || ' earns ' || to_char(salary,'$999999') || ' monthly,but wants ' || to_char(3*salary,'$999999') "Dream Salary" from employees

那这一行是一条数据,一行是一个别名

关于条件表达式的,使用decode函数产生以下一个结果select last_name "Last_name",job_id "Job_id",
decode(job_id,'AD_PRES','A','ST_MAN','B', 'IT_PROG','C', 'SA_REP','D', 'ST_CLERK','E') "Grade"
from employeesjob_id的不同人,这个部门grade叫A,按照下面的条件输出,Last_name还是在employees这个表里,那你就这样写,select last_name,job_id,然后看他job_id的grade,像是一个别名,使用decode(job_id),当你是AD_PRES这个部门的时候,我让你的grade,grade本身又是一个字符,可以这样写,如果是另一个部门,我就粘过来,逗号,B,我先把这个串写出来,这个部门第三个,第四个,第五个,当是这5个部门的时候,都给他叫E,剩下的是else,就不处理了,本身是什么就是什么,就这5个部门,这个括号相当于是一个end,以后取了一个别名,起个别名也叫Grade,from employeesselect last_name "Last_name",job_id "Job_id"decode(job_id,'AD_PRES','A','ST_MAN','B','IT_PROG','C','SA_REP','D','ST_CLERK','E') "Grade"from employees前面这个你也可以看成一个别名,如果你前面看成一个别名,你也可以写一下,叫"Last_name",因为它首字母大写,其他的都没有,Job_id,就这样呗

下面就有一个Grade,如果没有写的,Grade就是null,自己写的时候尽量写的规范一点,放在word里面格式需要稍微调一下,你一定要把这些例子亲自去写一写,并不难,但是呢,不敲的话容易忘,下面我们用case重写一遍8.	将第7题的查询用case函数再写一遍。
a)	select last_name "Last_name",job_id "Job_id",case job_id when 'AD_PRES'then 'A'
b)	when 'ST_MAN' then 'B'
c)	when 'IT_PROG' then 'C'
d)	when 'SA_REP' then 'D'
e)	when 'ST_CLERK' then'E' end  "Grade"
f)	from employeescase他,然后when,逗号不要,然后改成then,逗号去掉,小括号右括号相当于一个end,起个别名select last_name "Last_name",job_id "Job_id", case job_id when 'AD_PRES' then 'A'when 'IT_MAN' then 'B'when 'IT_PROG' then 'C'when 'SA_REP' then 'D'when 'ST_CLERK' then 'E' end 'Grade'from employees

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部