| 18. 打印出 "2009年10月14日 9:25:40" 格式的当前系统的日期和时间. |
| |
| select to_char(sysdate, 'YYYY"年"MM"月"DD"日" HH:MI:SS') |
| from dual |
| 注意: 使用双引号向日期中添加字符 |
| 19. 格式化数字: 1234567.89 为 1,234,567.89 |
| select to_char(1234567.89, '999,999,999.99') |
| from dual |
| 20. 字符串转为数字时 |
| 1). 若字符串中没有特殊字符, 可以进行隐式转换: |
| select '1234567.89' + 100 |
| from dual |
| 2). 若字符串中有特殊字符, 例如 '1,234,567.89', 则无法进行隐式转换, 需要使用 to_number() 来完成 |
| select to_number('1,234,567.89', '999,999,999.99') + 100 |
| from dual |
| 21. 对于把日期作为查询条件的查询, 一般都使用 to_date() 把一个字符串转为日期, 这样可以不必关注日期格式 |
| 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' |
| |
| 22. 转换函数: to_char(), to_number(), to_date() |
| |
| 23. 查询每个月倒数第 2 天入职的员工的信息. |
| |
| select last_name, hire_date |
| from employees |
| where hire_date = last_day(hire_date) - 1 |
| |
| 24. 计算公司员工的年薪 |
| |
| --错误写法: 因为空值计算的结果还是空值 |
| select last_name, salary * 12 * (1 + commission_pct) year_sal |
| from employees |
| |
| --正确写法 |
| select last_name, salary * 12 * (1 + nvl(commission_pct, 0)) year_sal |
| from employees |
| |
| 25. 查询部门号为 10, 20, 30 的员工信息, 若部门号为 10, 则打印其工资的 1.1 倍, 20 号部门, 则打印其工资的 1.2 倍, 30 号部门打印其工资的 1.3 倍数 |
| |
| --使用 case-when-then-else-end |
| select last_name, department_id, salary, case department_id when 10 then salary * 1.1 |
| when 20 then salary * 1.2 |
| when 30 then salary * 1.3 |
| end new_sal |
| from employees |
| where department_id in (10, 20, 30) |
| --使用 decode |
| select last_name, department_id, salary, decode(department_id, 10, salary * 1.1, |
| 20, salary * 1.2, |
| 30, salary * 1.3 |
| ) new_sal |
| from employees |
| where department_id in (10, 20, 30) |
| 1. 显示系统时间(注:日期+时间) |
| a) select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') |
| b) from dual |
| 2. 查询员工号,姓名,工资,以及工资提高百分之20%后的结果(new salary) |
| a) select employee_id,last_name,salary,salary*1.2 "new salary" |
| b) from employees |
| 3. 将员工的姓名按首字母排序,并写出姓名的长度(length) |
| a) select last_name,length(last_name) |
| b) from employees |
| c) 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 |
| 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 |
| 6. 做一个查询,产生下面的结果 |
| <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函数,按照下面的条件: |
| 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_PROG','C', 'SA_REP','D', 'ST_CLERK','E') "Grade" |
| from employees |
| 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 employees |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!