你可以得到当月的第一天,使用MM日期格式元素。
select to_char(trunc(sysdate, 'MM'), 'YYYY-MM-DD Hh24:MI:SS') from dual;
TO_CHAR(TRUNC(SYSDA
-------------------
2017-06-01 00:00:00
然后可以使用日期计算以添加天数或表示数的间隔,以获得每月第四天:
select to_char(trunc(sysdate, 'MM') + 3, 'YYYY-MM-DD Hh24:MI:SS') from dual;
TO_CHAR(TRUNC(SYSDA
-------------------
2017-06-04 00:00:00
如果你想要的数据高达那第四天开始,即达到23:59:59 3日,你可以看看值小于午夜4日:
select * from test.purchase
where create_ts >= trunc(sysdate, 'MM')
and create_ts < trunc(sysdate, 'MM') + 3;
您可以使用between,但因为这是包容性的,所以您需要在第3次指定绝对最新时间 - 检查列是日期还是时间戳,可能会更改,并且可能会有点混淆。如果您使用between trunc(sysdate, 'MM') and trunc(sysdate, 'MM') + 3,那么您将在4日午夜时包含任何记录,这不是您想要的。我发现使用>=和
如果列实际上是一个时间戳,那么你可以投的计算日期时间戳太,和/或上限的使用间隔:
select * from test.purchase
where create_ts >= cast(trunc(sysdate, 'MM') as timestamp)
and create_ts < cast(trunc(sysdate, 'MM') + 3 as timestamp);
...或:
...
and create_ts < cast(trunc(sysdate, 'MM') as timestamp) + interval '3' day;