oracle当月、当年、本周数据
- select * from table t
- where t.create_time >=TRUNC(SYSDATE, 'MM' )
- 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)
当年数据
- select * from table t
- where t.create_time >=trunc(sysdate, 'YYYY' )
- 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
本周(国外周日为一个星期第一天)
- 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
本周(国内周一为一个星期第一天)
- 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的原因无法识别大小写