to sum up:
1. Print out the date and time "October 14, 2009 9:25:40" format of the current system,
select to_char(sysdate,'yyyy"年"mm"月"dd"日" hh:mi:ss')
from dual;
--2020年01月08日 08:40:27
2. Format Digital: 1,234,567.89 to 1,234,567.89
select to_char(1234567.89,'000,000,000.00')
from dual;
-- 001,234,567.89
3. string to digital
--若字符串中没有特殊字符,可以进行隐式转换
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. For the date as a query of queries, usually using to_char () to a string to date, so do not have to concern the date format
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';
The transfer function: to_char (), to_number (), to_date ()
6. queries each month countdown information on day 2 of recruits.
select last_name,hire_date
from employees
where hire_date=last_day(hire_date)-1;
7. Calculate the annual salary of employees
select last_name,salary*12*(1+nvl(commission_pct,0)) year_sql
from employees;
8. query employee information department number 10, 20, if the department number is 10, then print 1.1 times their salary; 20 departments print 1.2 times their salary; 30 departments print 1.3 times their salary.
--使用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);
test:
1. Display system time (Note: date + time)
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss')
from dual;
--2020-01-08 09:29:05
2. Query the employee number, name, salary, and the salary increase 20%% The result was (new salary).
select employee_id,last_name,salary,salary*1.2 "new salary"
from employees;
3. The employee's name in alphabetical order, and write the name of the length (length).
select last_name,length(last_name)
from employees
order by last_name asc;
4. Query the names of the employees, and shows the number of months each employee in the company (worked_month).
select last_name,hire_date,round(months_between(sysdate,hire_date),1) worked_month
from employees;
5. Query employee's name, and the number of months of work in the company (worked_month), press the number of months in descending order.
select last_name,hire_date,round(months_between(sysdate,hire_date),1) worked_month
from employees
order by worked_month desc;
6. Make a query produces the following result: King earns $ 24000 monthly but wants $ 72000 alias Dream Salary..
select last_name || 'earns' || to_char(salary,'$999999') || 'monthly,but wants' || to_char(3*salary,'$999999') "Dream Salary"
from employees;
7. decode function under the conditions below.
job | grade |
---|---|
AD_PRES | A |
ST_MAN | B |
IT_PROG | C |
SA_REP | D |
ST_CLERK | E |
Produces the following results:
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. query question 7 with case function to write again.
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;