sql 查询上月最后一天17:00以后的数据

在orcal数据库,sql 查询上月最后一天17:00以后的数据
就是说,如果给一个日期,2012-10-11,要查询的区间为:
2012-9-30 17:00:00至2012-10-11 17:00:00,取这个区间的值
求赐教
如何写sql语句的where条件

第1个回答  2013-02-05
SQL> ALTER session SET nls_date_format='yyyy-mm-dd hh24:mi:ss';
会话已更改。

SQL> SELECT
2 TRUNC(SYSDATE) AS "今天",
3 LAST_DAY(TRUNC(SYSDATE)) AS "本月最后一天",
4 ADD_MONTHS(LAST_DAY(TRUNC(SYSDATE)), -1) AS "上个月最后一天",
5 ADD_MONTHS(LAST_DAY(TRUNC(SYSDATE)), -1) + 17/24 AS "上个月最后一天17:00"
6 FROM
7 dual;

今天 本月最后一天 上个月最后一天 上个月最后一天17:0
------------------- ------------------- ------------------- -------------------
2013-02-05 00:00:00 2013-02-28 00:00:00 2013-01-31 00:00:00 2013-01-31 17:00:00
第2个回答  2013-02-05
给你个思路吧
本月第一天会写吧 本月第一天的前一天就是上月最后一天
第3个回答  2013-02-05
between
TO_DATE(TO_CHAR(TRUNC(SYSDATE, 'mm') - 1, 'yyyy-mm-dd') ||
' 17:00:00',
'yyyy-mm-dd hh24:mi:ss')
and
TO_DATE(TO_CHAR(SYSDATE, 'yyyy-mm-dd') || ' 17:00:00',
'yyyy-mm-dd hh24:mi:ss')追问

我用的是oracle 9 ,是不是没这个函数呀TRUNC()

第4个回答  2013-02-05
select * from table where date between to_date('2012-9-30 17:00:00','YYYY-MM-DD HH24:MI:SS') and to_date('2012-10-11 17:00:00','YYYY-MM-DD HH24:MI:SS');追问

出现下列错误
Caused exception message is: translate sql exception, message is
format sql error. target database is 'Oracle9' detail message is :
ERROR
source sql is :
select * from T_ATS_AutoSaleIssue where fdate between to_date('2012-9-30 17:00:00','YYYY-MM-DD HH24:MI:SS') and to_date('2012-10-11 17:00:00','YYYY-MM-DD HH24:MI:SS')

追答

这条语句是不是写在一行? fdate是什么类型?是date类型吗?

追问

是date类型

追答

select * from T_ATS_AutoSaleIssue where fdate between to_date('2012-09-30 17:00:00','YYYY-MM-DD HH24:MI:SS') and to_date('2012-10-11 17:00:00','YYYY-MM-DD HH24:MI:SS') 月份不足2位用0补齐 这条语句是没有问题的,我在我oracle数据库试过了,只要你的fdate字段是date类型

第5个回答  2013-02-05
where v_date between last_day(add_months(to_date('2012-10-11 17:00:00','yyyy-mm-dd hh24:mi:ss'),-1)) and to_date('2012-10-11 17:00:00','yyyy-mm-dd hh24:mi:ss');本回答被提问者采纳