zoukankan      html  css  js  c++  java
  • SQL 小技巧 2


    ------查询倒数第4位是a的员工信息    

    where last_name like '%a___'
    --------查询没填email的员工信息 

       where email is null
    -----------查询1998年入职的员工信息  

     where hire_date between '1-1月-98' and '31-12月-98' where hire_date >= '1-1月-98' and hire_date<='31-12月-98'

    一、单行函数:运行在单条数据上,有一条数据函数就会被执行1次。
        1、字符串相关
            length(str)--获取字符串的长度
                --查询员工的姓的长度
                select last_name,length(last_name) from employees
                --查询长度是5位,并且第3位是a的员工
                select * from employees where length(last_name)=5 and last_name like '__a%'
                --显示字符串abcdefg的长度
                select length('abcdefg') from employees
                --dual 哑表,没有实际意义,只是为了select语句的语法完整性
                select length('abcdefg') from dual
            substr(str,begin,length)--从str中截取子串,从begin(下标从1开始)开始取长度为length个字符
                select substr('abcdefg',1,3) from dual
            lower(str)/upper(str)    --转小写、大写
                --查询姓中包含L的员工
                select * from employees where upper(last_name) like '%L%'
            concat(s1,s2)--字符串连接,等价于 s1 || s2
                --查询员工的全名
                select last_name || first_name from employees
                select concat(last_name, first_name) from employees
                select last_name || ' ' || first_name from employees
                select concat(concat(last_name, ' '), first_name) from employees
        2、数学相关
            mod(n1, n2) --求余,n1 % n2
                --查询编号是奇数的员工
                select * from employees where mod(employee_id, 2) = 1
            round(num, length)--四舍五入,对num四舍五入到小数点后length位
                --查询员工的日平均工资,保留2位小数
                select salary, round(salary/22 ,2) as 日平均工资 from employees
        3、日期相关
            sysdate --获取系统的当前日期时间
                select sysdate from dual
            to_char(date, '日期格式字符串')【重点】
                --按指定日期格式,把日期类型转成字符串类型
                --获取日期的不同部分,如年、月、日期
                日期格式字符串,包含如下字符:yyyy--4位年
                                              yy--2位年
                                              mm--月份
                                              dd-日期
                                              hh24--24时
                                              mi-分钟
                                              ss--秒
                                              d--星期(1-7)
                                              day-星期(星期天/Sunday)
                --按年月日时分秒显示当前日期
                select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from dual
                --查看员工的入职时间
                select hire_date from employees
                select to_char(hire_date, 'yyyy-mm') from employees
                --查看员工的入职月份
                select to_char(hire_date, 'mm') from employees
                --查看7月份入职的员工
                select * from employees where to_char(hire_date,'mm')=7
                --查看本月入职的员工
                select to_char(sysdate,'mm') from dual
                select * from employees where to_char(hire_date,'mm')=to_char(sysdate,'mm')
                --查看上个月入职的员工
                select * from employees where to_char(hire_date,'mm')=(to_char(sysdate,'mm')-1)--OK(有缺陷,如果是1月份-1后就变成0)
            add_months(date, num)--对月份进行加减,num为负,则为减
                --查看上个月入职的员工
                select add_months(sysdate,-1) from dual
                select * from employees where to_char(hire_date,'mm')=to_char(add_months(sysdate,-1),'mm')
                --查看昨天入职的员工
                select sysdate-1 from dual
            last_day(date)--获取date的最后1天
                select last_day(sysdate) from dual
            trunc(date, '日期格式字符串')--按指定的格式截断日期
                select trun(sysdate,'mm') from dual--获取当前月份的第1天
                select trun(sysdate,'yyyy') from dual--获取年的第1天
            to_date(str,'日期格式字符串')--根据给定的格式,把字符串转成日期
                select to_date('2011-5-6','yyyy-mm-dd') from dual
        4、处理null  nvl(n1,n2)--如果n1是null则取值n2,否则取值n1(n1==null? n2 : n1)
            --查看员工的年薪(月薪*12+奖金)
            select salary, salary*12, salary*12+commission_pct from employees    --ERROR
            select salary, salary*12, salary*12+nvl(commission_pct,0) from employees

  • 相关阅读:
    e.g.-basic-Si
    Telephone interview with Youyou Tu
    Mo2C-tag
    Usage of “symmetrical” and “symmetric”
    Xcrysden-2
    The partial charge density (1)
    利用 AWK 的数值计算功能提升工作效率(转载)
    扩展程序
    选择排序
    装饰递归函数
  • 原文地址:https://www.cnblogs.com/liuruipeng/p/7922051.html
Copyright © 2011-2022 走看看