oracle之单行函数之课后练习
生活随笔
收集整理的這篇文章主要介紹了
oracle之单行函数之课后练习
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
18. 打印出 "2009年10月14日 9:25:40" 格式的當(dāng)前系統(tǒng)的日期和時(shí)間.select to_char(sysdate, 'YYYY"年"MM"月"DD"日" HH:MI:SS')from dual 注意: 使用雙引號(hào)向日期中添加字符19. 格式化數(shù)字: 1234567.89 為 1,234,567.89select to_char(1234567.89, '999,999,999.99')from dual20. 字符串轉(zhuǎn)為數(shù)字時(shí)1). 若字符串中沒有特殊字符, 可以進(jìn)行隱式轉(zhuǎn)換:select '1234567.89' + 100from dual2). 若字符串中有特殊字符, 例如 '1,234,567.89', 則無(wú)法進(jìn)行隱式轉(zhuǎn)換, 需要使用 to_number() 來(lái)完成select to_number('1,234,567.89', '999,999,999.99') + 100from dual21. 對(duì)于把日期作為查詢條件的查詢, 一般都使用 to_date() 把一個(gè)字符串轉(zhuǎn)為日期, 這樣可以不必關(guān)注日期格式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'22. 轉(zhuǎn)換函數(shù): to_char(), to_number(), to_date()23. 查詢每個(gè)月倒數(shù)第 2 天入職的員工的信息. select last_name, hire_datefrom employeeswhere hire_date = last_day(hire_date) - 124. 計(jì)算公司員工的年薪--錯(cuò)誤寫法: 因?yàn)榭罩涤?jì)算的結(jié)果還是空值select last_name, salary * 12 * (1 + commission_pct) year_salfrom employees--正確寫法select last_name, salary * 12 * (1 + nvl(commission_pct, 0)) year_salfrom employees25. 查詢部門號(hào)為 10, 20, 30 的員工信息, 若部門號(hào)為 10, 則打印其工資的 1.1 倍, 20 號(hào)部門, 則打印其工資的 1.2 倍, 30 號(hào)部門打印其工資的 1.3 倍數(shù)--使用 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)
1. 顯示系統(tǒng)時(shí)間(注:日期+時(shí)間)
a) select to_char(sysdate,'yyyy-mm-dd hh:mi:ss')
b) from dual
2. 查詢員工號(hào),姓名,工資,以及工資提高百分之20%后的結(jié)果(new salary)
a) select employee_id,last_name,salary,salary*1.2 "new salary"
b) from employees
3. 將員工的姓名按首字母排序,并寫出姓名的長(zhǎng)度(length)
a) select last_name,length(last_name)
b) from employees
c) order by last_name asc
4. 查詢各員工的姓名,并顯示出各員工在公司工作的月份數(shù)(worked_month)。
a) select last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month
b) from employees
5. 查詢員工的姓名,以及在公司工作的月份數(shù)(worked_month),并按月份數(shù)降序排列
a) Select last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month
b) from employees
c) order by workded_month desc
6. 做一個(gè)查詢,產(chǎn)生下面的結(jié)果
<last_name> earns <salary> monthly but wants <salary*3>
Dream Salary
King earns $24000 monthly but wants $72000
select last_name || ' earns '|| to_char(salary,'$999999')||' monthly,but wants '||to_char(3*salary,'$999999') "Dream Salary"
from employees
7. 使用decode函數(shù),按照下面的條件:
job grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
產(chǎn)生下面的結(jié)果
Last_name Job_id Grade
king AD_PRES A
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 employees
8. 將第7題的查詢用case函數(shù)再寫一遍。
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 employees
?
?
總結(jié)
以上是生活随笔為你收集整理的oracle之单行函数之课后练习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 基于PHP的个人博客网站系统
- 下一篇: Python + OpenCv实现视频中