oracle 日期处理

oracle当月、当年、本周数据

当月数据
Sql代码 复制代码
  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)

当年数据
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  
select * from table t
where t.create_time >=trunc(sysdate,'YYYY') 
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   
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;

 

其他方式:取得当月数据

 

select t.*
  from tbl_order_history_info t
 where t.store_id = '1e5dace9-8772-4897-a63a-429b74155958' and
       t.trade_time between trunc(sysdate, 'month') and last_day(sysdate)

 

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

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

 

加法
select sysdate,add_months(sysdate,12) from dual;         --加1年
select sysdate,add_months(sysdate,1) from dual;         --加1月
select sysdate,to_char(sysdate+7,'yyyy-mm-dd HH24:MI:SS') from dual;    --加1星期
select sysdate,to_char(sysdate+1,'yyyy-mm-dd HH24:MI:SS') from dual;    --加1天
select sysdate,to_char(sysdate+1/24,'yyyy-mm-dd HH24:MI:SS') from dual;   --加1小时
select sysdate,to_char(sysdate+1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual;   --加1分钟
select sysdate,to_char(sysdate+1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; --加1秒

减法
select sysdate,add_months(sysdate,-12) from dual;         --减1年
select sysdate,add_months(sysdate,-1) from dual;         --减1月
select sysdate,to_char(sysdate-7,'yyyy-mm-dd HH24:MI:SS') from dual;    --减1星期
select sysdate,to_char(sysdate-1,'yyyy-mm-dd HH24:MI:SS') from dual;    --减1天
select sysdate,to_char(sysdate-1/24,'yyyy-mm-dd HH24:MI:SS') from dual;   --减1小时
select sysdate,to_char(sysdate-1/24/60,'yyyy-mm-dd HH24:MI:SS') from dual;   --减1分钟
select sysdate,to_char(sysdate-1/24/60/60,'yyyy-mm-dd HH24:MI:SS') from dual; --减1秒

 

 

oracle查找当天数据  应该设置为当天23:59:59

 

"from OrderHistoryInfo t where t.storeId=" + "'"
                                + storeId + "'"
                                + " and t.tradeTime between to_date(" + "'"
                                + startTime + "'"
                                + ",'yyyy-mm-dd') and to_date(" + "'" + endTime
                                + " 23:59:59'" + ",'yyyy-MM-dd HH24:mi:ss ')"   //格式必须为这样.因为plsql的原因无法识别大小写


  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值