zoukankan      html  css  js  c++  java
  • oracle SQL语句取本周本月本年的数据

    --国内从周一到周日 国外是周日到周六  
    select to_char(sysdate-1,'D') from dual;--取国内的星期几 去掉减一取国外的星期
    

      

    --取本周时间内的数据  
    select * from table  where DTIME >=trunc(next_day(sysdate-8,1)+1) and DTIME<=trunc(next_day(sysdate-8,1)+7)+1 ;  
       
    select * from table  where DTIME >=trunc(next_day(sysdate-8,1)) and DTIME<=trunc(next_day(sysdate-8,1)+7);--国外的  
         
    select * from table  where  DTIME >=TRUNC(SYSDATE, 'MM')  and DTIME<=last_day(SYSDATE);  
    --本月的  
    select * from table where to_char(DTIEM,'yyyy')=to_char(sysdate,'yyyy');  
    --本年的  
    -- 这样取的是 在一周内第几天,是以周日为开始的 
    select to_char(to_date('20130906','yyyymmdd'),'d') from dual; 
    --结果:6 注释:2013.09.06是周五,为本周的第六天 
    
    select to_char(sysdate+(2-to_char(sysdate,'d'))-7,'yyyymmdd') from dual;---上周一 
    select to_char(sysdate+(2-to_char(sysdate,'d'))-1,'yyyymmdd') from dual;---上周日 
    
    -- 一个更简单的写法 , 返回date类型 
    select trunc(sysdate,'iw') - 7 from dual;---上周一 
    select trunc(sysdate,'iw') - 1 from dual;--上周日 
    -- 取上个月最后一天  
    SELECT TO_CHAR(LAST_DAY(ADD_MONTHS(SYSDATE, -1)),'YYYYMMDD') FROM DUAL;  
    
    -- 取上个月第一天
    SELECT TO_CHAR(LAST_DAY(ADD_MONTHS(SYSDATE, -2)) + 1,'YYYYMMDD')  FROM DUAL;
  • 相关阅读:
    win10+PHP 安装memcache
    win10+PHP 安装redis
    一个github搞定微信小程序支付系列
    Linux下新建一个站点
    Linux下更改mysql版本
    零基础配置Linux服务器环境
    手把手教你使用ueditor
    react学习三
    react学习二 生命周期
    window.location.replace和window.location.href区别
  • 原文地址:https://www.cnblogs.com/linn/p/7383056.html
Copyright © 2011-2022 走看看