码迷,mamicode.com
首页 > 其他好文 > 详细

7月28

时间:2017-07-28 20:08:38      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:字符串   null   bsp   val   sysdate   explicit   ica   其他   strong   

选择部门30中的雇员

Select last_name, first_name from employees where department_id=30;

列出所有职员(CLERK)的姓名、编号和部门

Select last_name, first_name, employee_id, job_id from employees;

找出薪金大于5000的雇员

Select last_name, first_name from employees where salary>5000;

找出奖金高于0.1的雇员

Select last_name, first_name from employees where commission_pct>0.1;

找出部门50中的所有员工和部门30中的经理的详细资料

Select * from employees where department_id=50 or (department_id=30 and job_id like ‘%\_MAN’);

找出收取奖金的雇员的不同工作职位 每种职位显示一次   distinct

Sesect distinct job_id from employees where comission_pct is null;

找出不收取奖金或收取的工资低于5000的雇员

Select last_name, first_name from employees where commission_pct is null or salary*12*(commission_pct)<5000;

显示last_name不带有‘R‘的雇员姓名

Select  last_name, first_name from employees where last_name not like ‘%\_R’;

 

Order by排序 默认是升序   加上desc表示降序。

多行函数:

单行函数:

Character functions(字符串)函数:

Trunc截断,如 select last_name, trunc((sysdate - hire_date) /30) from employees;

Conversion 转换函数  implicat隐含类型转换   explicit明确类型转换   类型必须一致。

To_char做格式转换的使用。

Rr代表离当前年份最近的。Yy代表和当前年份在同一个世纪中。年份必须四个y

 

排序:

SQL> select last_name, salary from employees order by salary;

SQL> select last_name, salary from employees order by salary desc;

SQL> select last_name, salary from employees order by last_name;

SQL> select last_name, hire_date from employees order by hire_date;

SQL> select last_name, salary, commission_pct from employees order by salary desc, commission_pct desc;

SQL> select last_name, salary*12*(1+commission_pct) from employees order by 2;2表示第几列

SQL> select last_name, salary*12*(1+commission_pct) total_salary from employees order by total_salary;

 

 

单行函数

SQL> select upper(first_name), lower(last_name), length(last_name) from employees;

 

SQL> select (sysdate-hire_date)/7 from employees;

SQL> select trunc((sysdate-hire_date)/30, 0) from employees;

SQL> select trunc(months_between(sysdate,hire_date), 0) from employees;

SQL> select sysdate+3650 from dual;

SQL> select add_months(sysdate, 120) from dual;

SQL> select next_day(‘2015-09-01‘, ‘friday‘) from dual;

SQL> select next_day(‘2015-10-01‘, 6) from dual;

SQL> select last_day(sysdate) from dual;

 

SQL> select round(to_date(‘2015-10-10‘,‘yyyy-mm-dd‘), ‘MONTH‘) from dual;

SQL> select round(to_date(‘2015-10-16‘,‘yyyy-mm-dd‘), ‘MONTH‘) from dual;

SQL> select round(to_date(‘2015-10-10‘,‘yyyy-mm-dd‘), ‘YEAR‘) from dual;

SQL> select round(sysdate, ‘DAY‘) from dual;

 

练习:

找出各月最后三天内受雇的所有雇员

extract(month from hire_date+4) != extract(month from hire_date)

找出早于25年之前受雇的雇员

months_between(sysdate, hire_date)/300>=25

显示正好为6个字符的雇员姓名

length(last_name)=6

显示所有雇员的姓名的前三个字符

substr(last_name, 1, 3)

显示所有雇员的姓名,用a替换所有‘A‘

replace(last_name, ‘A‘, ‘a‘)

类型转换和其他函数

SQL> select to_char(salary, ‘$999,999.00‘) from employees;

SQL> select last_name, to_char(hire_date, ‘dd-Mon-RR‘) from employees;

SQL> select to_char(sysdate, ‘yyyy-mm-dd hh24:mi:ss‘) from dual;

SQL> select to_char(sysdate, ‘yyyy-mm-dd hh:mi:ss AM‘) from dual;

SQL> select last_name from employees where hire_date=to_date(‘2006-05-23‘, ‘yyyy-mm-dd‘);

SQL> select to_number(‘$123,456.78‘, ‘$999,999.00‘) from dual;

 

练习:

查询2006年入职员工:

select last_name

from employees

where hire_date between to_date(‘2006-01-01‘, ‘yyyy-mm-dd‘)

and  to_date(‘2006-12-31‘, ‘yyyy-mm-dd‘);

 

select last_name

from employees

where to_char(hire_date, ‘yyyy‘)=‘2006‘;

 

select last_name

from employees

where extract(year from hire_date)=2006;

 

--不推荐

select last_name

from employees

where hire_date like ‘2006%‘;

 

查询历年9月份入职的员工:

select last_name

from employees

where to_char(hire_date, ‘mm‘)=‘09‘;

 

select last_name

from employees

where extract(month from hire_date)=9;

 

其他函数:

nvl

nvl(val1, val2)

if val1 is not null

then

    return val1;

else

    return val2;

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

 

练习:

显示所有员工部门编号,没有部门的显示未分配部门

Select last_name, nvl(to_char(department_id), ‘N/A’)  from employees;

 

 

casedecode

IT_PROG +1000

SA_REP +1500

ST_CLERK +2000

其他人工资不变    select salary from employees where job_id not in=‘IT_PROG‘;  SA_REP      ST_CLERK

 

select salary+1000 from employees where job_id=‘IT_PROG‘;

 

select last_name, job_id, salary,

case job_id

  when ‘IT_PROG‘ then salary+1000

  when ‘SA_REP‘ then salary+1500

  when ‘ST_CLERK‘ then salary+2000

  else salary

end new_salary

from employees;

 

select last_name, job_id, salary,

decode( job_id,

  ‘IT_PROG‘, salary+1000,

  ‘SA_REP‘,  salary+1500,

  ‘ST_CLERK‘, salary+2000,

  salary) new_salary

from employees;

练习:

按照员工工资,对员工分级显示:

A 20001-25000

B 15001-20000

C 10001-15000

D 5001-10000

E 0-5000

select last_name, salary, (case when salary>=20001 and salary<=25000 then ‘A‘  when salary>=15001 and salary<=20000 then ‘B‘  when salary>=10001 and salary<=15000 then ‘C‘ when salary>=5001 and salary<=10000 then ‘D‘  when salary>=0 and salary<=5000 then ‘E‘ end) qualified_salary from employees;

 

 

7月28

标签:字符串   null   bsp   val   sysdate   explicit   ica   其他   strong   

原文地址:http://www.cnblogs.com/m4116/p/7252225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!