Oracle从小白到精通第一天(单行函数,以及date,char,number之间的转换)(尚硅谷学习Oracle笔记)

1.基本关键字

  1. desc 表名 意思是描述一个表结构
  2. order by desc或者asc 降序或者升序排序 一般在select语句的结尾

2.select基本用法以及其中一些基本知识

  1. select * from employees where 条件
  2. 算数运算符 + - * / 数字和日期可以使用,但是日期只能使用 + -
    其中日期应该这样写
    select last_name,hire_date from employees where to_char(hire_date,'yyyy-mm-dd')='1994-06-07';select 8*4 from dual; 这样来计算一个数
  3. 空值包含的数学表达式都为空值
  4. 列的别名有三种表示方法,第一种 直接空格 后面跟别名,第二种 用as ,第三种用 “ ”来表示。
  5. 连接符 || 可以把不同的列连接成一列 select first_name||last_name from employees;
  6. 删除重复行使用 distinct关键字 select distinct department_id from employees;

3.过滤排序数据使用的是where 条件

  1. 其中有=, >,< ,>=,<=,<>huozhe!=;这么几种
  2. where语句要紧跟着from语句
  3. 字符和日期要在单引号中,并且对大小写比较敏感
  4. 其他的比较运算符有
    1.between … and … 在两个范围之间,包含两个边界值
    2.in (…,…,…) 等于列表值中的一个
    3.like 模糊查询 %是匹配所有 _匹配一个 其中有个转义字符 \ escape ’ \ ’
    select job_id from jobs where job_id like ‘IT\_%’ escape ‘\’;
    4.is null 表示空值的意思
  5. 逻辑运算主要有三个 and 并 or 或 not否

4.单行函数

单行函数最基本的解释就是 对一行进行变换,每行返回一个结果

  1. 大小写控制函数
    1.lower 小写
    2.upper 大写
    3.initcap 首字符大写
    select * from employees where lower (last_name)=‘king’;当你不知道名字是否是大小写的时候可以用lower或者upper来变化
  2. 字符控制函数
    1.concat
    2.substr
    3.length
    select concat(‘hello’,‘world’),substr(‘helloworld’,2,4),length(‘helloworld’)
    from dual
    上面这三个函数的作用分别是 连接两个字符,截取相应的字符,从第几个开始截取几个,一个字符的长度。
    4.instr 判读某个字符在字符串中首次出现的位置
    select instr(‘hello world’, ‘d’) from dual;
    5.lpad|rpad 左对齐 右对齐
    select employee_id ,last_name,lpad(salary,10,’*’) from employees;
    salary 有10位, 不足10 用 * 左补齐, rpad反之
    6.trim
    移除首位和末尾某个字符 select trim('h’from ‘hhhhellohworldh’) from dual 结果是ellohworld
    7.replace
    替换某个字符***所有*** select replace(‘weofoiweweww’,‘w’,‘q’) from dual 结果是qeofoiqeqeqq
  3. 数字函数
    1.round 四舍五入
    select round(1516.54,2),round(16496.99),round(5615.55,-2) from dual;
    结果是1516.54 # 16497 # 5600
    2.trunc 截断也就是舍去 select trunc(1516.54,1),trunc(16496.99),trunc(5615.55,-1) from dual结果是1516.5 # 16496 # 5610
    3.mod 求余 select mod(1100,100)from dual; 结果是0
  4. 日期函数 sysdate
    日期函数中既包含日期也包含时间
    1.months_between 两个日期之间的月数months_between(sysdate,hire_date)
    2.add_months向指定日期加上若干月数add_months(sysdate,2)
    3.next_day 最近的星期几是多少 next_day(sysdate,‘星期日’)
    4.last_day本月的最后一天
    来公司的员工中,hire_date 是每个月倒数第二天来公司的有哪些
    select last_name,hire_date from employees where hire_date=last_day(hire_date)-1;
  5. 转换函数
    1.date型可以与varchar2相互转换,varchar2也可以与number相互转换
    在这里插入图片描述
    2.date转换成char
    select employee_id,hire_date
    from employees
    where to_char(hire_date,‘yyyy-mm-dd’)=‘1994-06-07’;
    更改输出使其输出1194年6月7日的样子
    select employee_id,to_char(hire_date,‘yyyy"年"mm"月"dd"日"’)
    from employees
    where to_char(hire_date,‘yyyy"年"mm"月"dd"日"’)=‘1994年06月07日’
    3.char转换成date
    select employee_id,hire_date
    from employees
    where to_date(‘1994-06-07’,‘yyyy-mm-dd’)=hire_date;
    另一种写法
    select employee_id,hire_date
    from employees
    where hire_date=to_date(‘1994-06-07’,‘yyyy-mm-dd’);
    3.char转换成number
    select to_number(’$23,265,465.23’,’$999,999,999.99’)
    from dual;
    4.number转换成char
    这样就能把一个数字转换成三个一位的字符串,,,可以用9也可以用0,不过用0前面会补足0,用两种一种是前面加L表示本地的金钱符号,一种是$美元的金钱符号
    select to_char(23423423.323,‘L999,999,999,999,999.99’)
    from dual
  6. 通用函数
    这些函数适用于任何数据类型,同时也适用于空值
    1.nvl(expr1,expr2) 当expr1为空时就替换成expr2,如果不为空,就不做处理
    将空值转换成已知的值,数据类型有日期,字符,数字
    ----求一个员工的总年薪包括奖金率(commission_pct)
    select employee_id,salary12(1+nvl(commission_pct,0))
    from employees
    2.nvl2(expr1,expr2,expr3) 当1不为空返回2,当1为空返回3
    3.nullif(expr1,expr2) 当两个相等的时候返回null,不相等的时候返回expr1;
    4.coalesce(expr1,expr2,…,exprn)如果1为空返回2,如果2为空返回3,如果3为空,返回4.。。。。。。
  7. 条件表达式
    条件表达式有两种 :一种是case表达式,一种是decode函数
    在这里插入图片描述
    练习:查询部门号为 10, 20, 30 的员工信息, 若部门号为 10, 则打印其工资的 1.1 倍, 20 号部门, 则打印其工资的 1.2 倍, 30 号部门打印其工资的 1.3 倍数

select employee_id,department_id,last_name,
case department_id when 10 then salary*1.1
when 20 then salary*1.2
when 30 then salary*1.3
end new_salary
from employees
where department_id in(10,20,30)
在这里插入图片描述
select employee_id,department_id,last_name,decode(department_id,10,salary*1.1,
20,salary*1.2,
salary*1.3) new_salary
from employees
where department_id in(10,20,30)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值