oracle 时间格式

来源:互联网 发布:大数据爬虫 编辑:程序博客网 时间:2024/04/28 07:13

oracle当月、当年、本周数据  

2009-07-26 13:26:32|  分类:oracle |字号 订阅

当月数据
Sql代码 复制代码
  1. select * from table t    
  2. where t.create_time >=TRUNC(SYSDATE, 'MM')    
  3. and t.create_time<=last_day(SYSDATE)  
Sql代码
  1. select * from table t   
  2. where t.create_time >=TRUNC(SYSDATE, 'MM')   
  3. and t.create_time<=last_day(SYSDATE)  

当年数据
Sql代码 复制代码
  1. select * from table t   
  2. where t.create_time >=trunc(sysdate,'YYYY')    
  3. and t.create_time<=add_months(trunc(sysdate,'YYYY'),12)-1  
Sql代码
  1. select * from table t  
  2. where t.create_time >=trunc(sysdate,'YYYY')   
  3. and t.create_time<=add_months(trunc(sysdate,'YYYY'),12)-1  

本周(国外周日为一个星期第一天)
Sql代码 复制代码
  1. where t.create_time >=trunc(sysdate,'day')+1 and t.create_time<=trunc(sysdate,'day')+6   
Sql代码
  1. where t.create_time >=trunc(sysdate,'day')+1 and t.create_time<=trunc(sysdate,'day')+6   

本周(国内周一为一个星期第一天)
Sql代码 复制代码
  1. where t.create_time >=trunc(next_day(sysdate-8,1)+1) and t.create_time<=trunc(next_day(sysdate-8,1)+7)+1  

oracle 取当月最后一天的sql语句:

select last_day(sysdate) from dual;

add_months:在当前日期的基础上加/减月份,返回加/减后的日期。

SQL> select add_months(sysdate, -3) three_months_ago from dual;




处理月份天数不定的办法
      select to_char(add_months(last_day(sysdate) +1, -2),
      ''yyyymmdd''),last_day(sysdate) from dual

     
      找出今年的天数
      select add_months(trunc(sysdate,''year''), 12) - trunc(sysdate,''year'')
      from dual

      闰年的处理方法
      to_char( last_day( to_date(''02'' || :year,''mmyyyy'') ), ''dd'' )
      如果是28就不是闰年


原创粉丝点击