• 数据库(四)日期查询


    日期函数
    now()    获取系统时间
    date_format(date,pattern)   时间格式化为字符串
    str_to_date(str,pattern)    字符串格式化为时间
    year(date)   获取年份
    month(date)   获取月份
    day(date)   获取天数
    to_days(date)   获取1970至date的天数
    date_add(date, interval    int    unit)   增加指定单位的时间
    datediff(date,date)   获取两个时间的天数差

    -- %Y是年%m是月%D是日%H小时%i分钟%s秒

    -- 获取当前时间

    select now()

    -- 获取对应的时间单位

    select year(now())
    select month(now())
    select day(now())

    -- %Y是年%m是月%D是日%H小时%i分钟%s秒
    -- 对日期进行格式化

    select date_format(now(),'%Y-%m-%D %H:%i;%s')

    -- 获取1970年至今的总天数

    select to_day(now());

    -- 获取当前时间在本年度的天数

    select dayofyear(now());

    -- 今天入职的时间

    -- 方法一:
    select * from emp where date_format(now(),'%Y%m%d')=date_format(hire_date,'%Y%m%d');
    -- 方法二:
    select * from emp where to_days(now())=to_days(hire_date);

    -- 对指定的时间单位进行增减计算  date_add(date, interval    int    unit)

    -- 加上天数
    select date_add(now(),interval 2 month);
    -- 减掉天数
    select date_add(now(),interval -2 day);

    -- 查询上个月最后一天入职的员工  date_add(date, interval    int    unit)

    select date_add(now(), interval  -day(now())  day)

    -- 查询这个月第一天入职的员工  date_add(date, interval    int    unit)

    select date_add(now(),interval  -day(now())  day)
  • 相关阅读:
    Oracle诊断:在程序的运行中,有时候数据库会断开连接
    Linux shell
    Java继承和多态-Static关键字
    Oracle诊断:使用USER_SEGMENTS分配给表的物理空间大小
    Linux shell -查找字符(find,xargs,grep)
    Linux shell
    Linux shell
    Linux shell
    Java heap size
    Oracle诊断: 服务器启后,无法连接
  • 原文地址:https://www.cnblogs.com/gfl-1112/p/12734770.html
Copyright © 2020-2023  润新知