oracle 查询本周,本月,本年数据

来源:互联网 发布:linux 字符串连接 编辑:程序博客网 时间:2024/05/10 21:27

oracle 查询本周,本月,本年数据

当月数据

Java代码
  1. select * from table t       
  2. where t.create_time >=TRUNC(SYSDATE, 'MM')       
  3. and t.create_time<=last_day(SYSDATE)    
select * from table t where t.create_time >=TRUNC(SYSDATE, 'MM') and t.create_time<=last_day(SYSDATE)Java代码
  1. create_time为你要查询的时间  
create_time为你要查询的时间

当年数据

Java代码
  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  
select * from table t where t.create_time >=trunc(sysdate,'YYYY') and t.create_time<=add_months(trunc(sysdate,'YYYY'),12)-1

本周(国外周日为一个星期第一天)

Java代码
  1. where t.create_time >=trunc(sysdate,'day')+1 and t.create_time<=trunc(sysdate,'day')+6    
where t.create_time >=trunc(sysdate,'day')+1 and t.create_time<=trunc(sysdate,'day')+6

本周(国内周一为一个星期第一天)

Java代码
  1. where t.create_time >=trunc(next_day(sysdate-8,1)+1) and t.create_time<=trunc(next_day(sysdate-8,1)+7)+1  
where t.create_time >=trunc(next_day(sysdate-8,1)+1) and t.create_time<=trunc(next_day(sysdate-8,1)+7)+1


--获取本周数据

select *,DATEPART(W,date1) as weeks from dates whereDATEDIFF(wk,date1,GETDATE())=0;

--不过这样获取的周是以星期日开始的,即包括周日~周六,而不是周一~周日,可以通过相差天数来获取

select from dates where DATEDIFF(d,date1,GETDATE())>=andDATEDIFF(d,date1,GETDATE())<5;

 

 

--获取本月数据

select * from dates where DATEDIFF(mm,date1,GETDATE())=0;

--获取本月数据上一月的将0改为1

 

--获取上一年数据

select * from dates where DATEDIFF(yyyy,date1,GETDATE())=1;


0 0
原创粉丝点击