简体   繁体   English

Oracle SQL从月底-16日和15日-1日检索数据

[英]Oracle SQL Retrieve Data From End of Month - 16th And 15th - 1st

Alright so I am trying to retrieve data a field we will call DATE_OF_ENTRY and the field is like this. 好吧,所以我试图检索一个我们称为DATE_OF_ENTRY的字段的数据,而该字段就是这样。

Example DATE_OF_ENTRY Data 示例DATE_OF_ENTRY数据

28-NOV-15

So I need to use this field in a script that will be running twice a month to pull certain records. 因此,我需要在每月运行两次的脚本中使用此字段来提取某些记录。 Basically when it's the 16th day of the current month I want all the records from the 1st-15th to be pulled up. 基本上是当月的第16天,我希望从1月15日开始收集所有记录。 When I run this script on the 1st of the next month I want all the records from the 16th-End of last month. 当我在下个月的1月1日运行此脚本时,我希望获得上个月16日末的所有记录。

What I am using now 我现在在用什么

WHERE ROUND(DATE_OF_ENTRY,'MM') = ROUND(sysdate-1,'MM') AND DATE_OF_ENTRY < trunc(sysdate)

The problem with this statement is that it works on the 1st for the 16th to End of the last month, but on the 16th it gets data from the prior month still. 该语句的问题在于它在上个月的16号到最后一个月的1号工作,但在16号仍然从上个月获取数据。

Any help is appreciated! 任何帮助表示赞赏!

Using TRUNC() function with MONTH parameter will get the first day of the month. TRUNC()函数与MONTH参数一起使用将获得该MONTH的第一天。

Using TRUNC() function with DATE_OF_ENTRY will remove the TIME part. TRUNC()函数与DATE_OF_ENTRY一起DATE_OF_ENTRY将删除TIME部分。

Use + operator to add days to a DATE 使用+运算符将DATE添加到DATE

 SELECT TRUNC(sysdate, 'MONTH') firstDay,
        TRUNC(sysdate, 'MONTH') + 15 Day15,
        *
 FROM yourTable
 WHERE TRUNC(DATE_OF_ENTRY) >= TRUNC(sysdate, 'MONTH')
   AND TRUNC(DATE_OF_ENTRY) <= TRUNC(sysdate, 'MONTH') + 15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM