第三章 單行函數 總結與測試

總結:

1.打印出"2009年10月14日 9:25:40“格式的當前系統的日期和時間,

select to_char(sysdate,'yyyy"年"mm"月"dd"日" hh:mi:ss') 
	from dual;
	--2020年01月08日 08:40:27

2.格式化數字:1234567.89爲1,234,567.89

select to_char(1234567.89,'000,000,000.00') 
	from dual;
-- 001,234,567.89

3.字符串轉爲數字時

--若字符串中沒有特殊字符,可以進行隱式轉換
select '1234567.89'+100 
	from dual;
	--1234667.89
--若字符串中有特殊字符,例如'1,234,567.89',則無法進行隱式轉換,需要使用to_number()來完成
select to_number('1,234,567.89', '999,999,999.99')+100
	from dual;
	--1234667.89

4.對於把日期作爲查詢條件的查詢,一般都使用to_char()把一個字符串轉爲日期,這樣可以不必關注日期格式

select last_name,hire_date
	from employees
	where hire_date=to_date('1998-5-23','yyyy-mm-dd');
	--where to_char(hire_date,'yyyy-mm-dd')='1998-5-23';

5.轉換函數:to_char(),to_number(),to_date()

6.查詢每個月倒數第2天入職的員工的信息。

select last_name,hire_date
	from employees
	where hire_date=last_day(hire_date)-1;

7.計算公司員工的年薪

select last_name,salary*12*(1+nvl(commission_pct,0)) year_sql
	from employees;

8.查詢部門號爲10,20,30的員工信息,若部門號爲10,則打印其工資的1.1倍;20號部門則打印其工資的1.2倍;30號部門打印其工資的1.3倍。

--使用case-when-then-else
select employee_id,last_name,case department_id when 10 then salary*1.1 
												when 20 then salary*1.2 
												else  salary*1.3 end new_sal
	from employees
	where department_id in (10,20,30);
--使用edcode函數
select employee_id,last_name,department_id,decode(department_id,10,salary*1.1,
                                                 			    20,salary*1.2
                                                 			    salary) new_sal
	from employees
	where department_id in (10,20,30);
測試:

1.顯示系統時間(注:日期+時間)

select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') 
	from dual;
	--2020-01-08 09:29:05

2.查詢員工號,姓名,工資,以及工資提高百分之20%後的結果(new salary)。

select employee_id,last_name,salary,salary*1.2 "new salary"
	from employees;

3.將員工的姓名按首字母排序,並寫出姓名的長度(length).

select last_name,length(last_name)
	from employees
	order by last_name asc;

4.查詢各員工的姓名,並顯示出各員工在公司工作的月份數(worked_month).

select last_name,hire_date,round(months_between(sysdate,hire_date),1) worked_month
from employees;

5.查詢員工的姓名,以及在公司工作的月份數(worked_month),並按月份數降序排列。

select last_name,hire_date,round(months_between(sysdate,hire_date),1) worked_month
from employees
order by worked_month desc;

6.做一個查詢,產生下面的結果:King earns $24000 monthly but wants $72000.別名爲Dream Salary。

select last_name || 'earns' || to_char(salary,'$999999') || 'monthly,but wants' || to_char(3*salary,'$999999') "Dream Salary"
from employees;

7.使用decode函數,按照下面的條件。

job grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E

產生下面的結果:

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_PROGC','C'
                             							   'SA_REP','D',
                                      					   'ST_CLERK','E') "Grade"
  from employees;                                     

8.將第7題的查詢用case函數在寫一遍。

select last_name "Last_name",job_id "Job_id",case job_id when'AD_PRES'then'A'
                                     				     when 'ST_MAN'then'B'
                                     				     when 'IT_PROGC'then'C'
                             						     when'SA_REP'then'D'
                                      				     when'ST_CLERK'then'E' end "Grade"
from employees;      
發佈了8 篇原創文章 · 獲贊 13 · 訪問量 3481
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章