zoukankan      html  css  js  c++  java
  • sql基础知识:日期的常用用法

    日期操作

    select sysdate,add_months(sysdate,12) from dual; -- + 1 year
    select sysdate,add_months(sysdate,1) from dual; -- + 1 month
    select sysdate,to_char(sysdate+7,'yyyy-mm-dd HH24:MI:SS') from dual; -- + 1 week
    select sysdate,to_char(sysdate+1,'yyyy-mm-dd HH24:MI:SS') from dual; -- + 1 day
    select sysdate,to_char(sysdate+1/24,'yyyy-mm-dd HH24:MI:SS') from dual; -- + 1 hour
    select sysdate,to_char(sysdate+1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual; -- + 1 min
    select sysdate,to_char(sysdate+1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; -- + 1 second
    
    select sysdate,add_months(sysdate,-12) from dual; -- - 1 year
    select sysdate,add_months(sysdate,-1) from dual; -- - 1 month
    select sysdate,to_char(sysdate-7,'yyyy-mm-dd HH24:MI:SS') from dual; -- - 1 week
    select sysdate,to_char(sysdate-1,'yyyy-mm-dd HH24:MI:SS') from dual; -- - 1 day
    select sysdate,to_char(sysdate-1/24,'yyyy-mm-dd HH24:MI:SS') from dual; -- - 1 hour
    select sysdate,to_char(sysdate-1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual; -- - 1 min
    select sysdate,to_char(sysdate-1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; -- - 1 second
    

    日期处理

    select sysdate,to_char(sysdate,'yyyy-mm-dd HH24:MI:SS') from dual;
    
    select sysdate,to_char(sysdate,'yyyy') from dual;--年
    select sysdate,to_char(sysdate,'Q') from dual;--季度
    select sysdate,to_char(sysdate,'mm') from dual;--月
    select sysdate,to_char(sysdate,'dd') from dual;--日
    select sysdate,to_char(sysdate,'ddd') from dual;--年中的第几天
    select sysdate,to_char(sysdate,'WW') from dual;--年中的第几个星期
    select sysdate,to_char(sysdate,'W') from dual;--该月的第几个星期
    select sysdate,to_char(sysdate,'D') from dual;--周中的第几天
    select sysdate,to_char(sysdate,'hh') from dual;--12进制 的小时
    select sysdate,to_char(sysdate,'hh24') from dual;--24进制 的小时
    select sysdate,to_char(sysdate,'Mi') from dual;--分钟
    select sysdate,to_char(sysdate,'ss') from dual;--秒
    

    常用的日期操作

    -- 得到当前的日期
    select sysdate from dual;
    
    -- 得到当天凌晨0点0分0秒的日期
    select trunc(sysdate) from dual;
    
    -- 得到当天的最后一秒 x年x月x日 23:59:59
    select trunc(sysdate)+0.99999 from dual;
    
    -- 得到明天凌晨0点0分0秒的日期
    select trunc(sysdate)+1 from dual;
    
    -- 得到本月1号的日期
    select trunc(sysdate,'mm') from dual;
    
    -- 得到下个月1号的日期
    select trunc(add_months(sysdate,1),'mm') from dual; 
    
    -- 得到当月的最后一天
    select last_day(sysdate),
           last_day(trunc(sysdate)),
           trunc(last_day(sysdate)),
           trunc(add_months(sysdate,1),'mm') - 1 
    from dual; 
    
    select * from all_objects;
    
    -- 得到一年的每一天
    select trunc(sysdate,'yyyy')+ rn -1 date0 
    from 
    (select rownum rn from all_objects 
    where rownum<366); 
    
    -- 判断是闰年还是平年
    select decode(to_char(last_day(trunc(sysdate,'y')+31),'dd'),'29','闰年','平年') from dual; 
    -- 解析
    select trunc(sysdate,'y') from dual;--获取当前年份的1月1日
    select trunc(sysdate,'y')+31 from dual;--获取当前年份的2月1日
    select to_char(last_day(trunc(sysdate,'y')+31),'dd') from dual;--获取2月份的最后一天
    

    注意

    • ALL_OBJECTS describes all objects accessible to the current user. 描述当前用户有访问权限的所有对象
    • DBA_OBJECTS describes all objects in the database. 描述了数据库中的所有对象
    • USER_OBJECTS describes all objects owned by the current user. 描述了当前用户所拥有的所有对象

    千万不要觉得all_和dba_视图都是所有对象的意思, all_和权限有关;
    所以出现【不同用户访问all_objects视图,相同过滤条件,结果不同 (例如:"A用户访问all_objects视图过滤B.T1表有数据,而C用户也访问all_objects视图过滤B.T1表却没有数据" )】是正常的,是因为C用户没有访问B.T1表权限,用dba_objects可以解决;
    所以适当的选择 dba_
    和all_*视图。

    参考

  • 相关阅读:
    I NEED A OFFER!
    水题 Codeforces Round #303 (Div. 2) A. Toy Cars
    模拟 HDOJ 5099 Comparison of Android versions
    模拟 HDOJ 5095 Linearization of the kernel functions in SVM
    贪心 HDOJ 5090 Game with Pearls
    Kruskal HDOJ 1863 畅通工程
    Kruskal HDOJ 1233 还是畅通工程
    并查集 HDOJ 1232 畅通工程
    DFS/并查集 Codeforces Round #286 (Div. 2) B
    水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift
  • 原文地址:https://www.cnblogs.com/xing901022/p/5950298.html
Copyright © 2011-2022 走看看