简体   繁体   English

oracle sql中与日期比较的月份和年份字符串值

[英]month and year string value compared to date in oracle sql

have 2 columns in table 表中有2列

id  CLOSED_DATE
1   11/01/2017
2   12/01/2017
3   12/02/2017
4   01/01/2018
5   02/01/2018

I have a string value of month and year in format "DEC-2017", how do i find which all is in previous months (Id: 1) and in Next months (id:4,5) 我的月份和年份的字符串值的格式为“ DEC-2017”,如何查找前几个月(Id:1)和下个月(id:4,5)的所有值

i tried like below, but it gave me id 3,4 and 5, because TO_DATE('DEC-2017','MON-yyyy') converted to 01-DEC-2017. 我尝试如下所示,但它给了我ID 3,4和5,因为TO_DATE('DEC-2017','MON-yyyy')转换为01-DEC-2017。

select id from table where TO_DATE('DEC-2017','MON-yyyy') > CLOSED_DATE;

Just throw in an add_months() : 只需添加一个add_months()

select id
from table
where add_months(TO_DATE('DEC-2017','MON-yyyy'), 1) >= CLOSED_DATE;

You want all months except for the one given? 除了给定的那一个月,您要所有的月吗? Then convert closed_date and compare it with the given string. 然后转换closed_date ,并将其与给定的字符串进行比较。

select id 
from table 
where to_char(closed_date, 'MON-yyyy', 'nls_date_language=english') <> 'DEC-2017';

Do what Gordon Said, but Modify the Conditions Slightly 照戈登所说的做,但要稍微修改条件

let us create a sample table, smple_t: 让我们创建一个示例表smple_t:

    SCOTT@db>CREATE TABLE smple_t
      2      AS
      3          SELECT
      4              ROWNUM id,
      5              ( trunc(SYSDATE) + power(-1,mod(t.rndm,2) ) * t.rndm ) closed_date
      6          FROM
      7              (
      8                  SELECT
      9                      ROWNUM id,
     10                      round(dbms_random.value *-30,0) rndm
     11                  FROM
     12                      dual
     13                  CONNECT BY
     14                      level <= 400
     15              ) t;

Table SMPLE_T created.

Let us check record count: 让我们检查记录数:

SCOTT@db>SELECT
  2      COUNT(1) 
  3  FROM
  4      smple_t;
  COUNT(1) 
       400 

Now let us look at the records 1 month preceding DEC-1-2017 to 1 month after (I assume entire month of January). 现在让我们来看一下DEC-1-2017之前1个月到之后1个月的记录(我假设是整个1月)。

The "trick" is to make sure if you want all of November that you use the >= operator for the lower boundary and use the < operator for the upper boundary: “技巧”是确保是否要在11月的整个月中,将>=运算符用于下边界,将<运算符用于上边界:

SCOTT@db>SELECT
  2      COUNT(1),
  3      add_months(TO_DATE('DEC-2017','MON-YYYY'),-1) all_nov_dates,
  4      add_months(TO_DATE('DEC-2017','MON-YYYY'),2) all_jan_dates
  5  FROM
  6      smple_t
  7  WHERE
  8      1 = 1
  9      AND   closed_date < add_months(TO_DATE('DEC-2017','MON-YYYY'),2)
 10      AND   closed_date >= add_months(TO_DATE('DEC-2017','MON-YYYY'),-1)
 11  GROUP BY
 12      add_months(TO_DATE('DEC-2017','MON-YYYY'),-1),
 13      add_months(TO_DATE('DEC-2017','MON-YYYY'),2);
  COUNT(1) ALL_NOV_DATES             ALL_JAN_DATES             
---------------------------------------------------
       207 01-NOV-2017 12:00:00 AM   01-FEB-2018 12:00:00 AM   

Addendum based on update to requirement: 根据要求更新的附录:

OP indicated that the desired result set is non-December 2017 (or 2016) closed dates. OP表示期望的结果集是2017年12月(或2016年)非截止日期。

OP indicated, "It should give me all months id before Dec-2016 excluding Dec-2016.... Nov-16, Oct-16.. and another one result with all future months excluding Dec-2016, ie, Jan-17,Feb-17" OP指出:“应该给我2016年12月之前的所有月份编号,但不包括Dec-2016 .... Nov-16,Oct-16 ..以及另一个结果,包括除2016年12月之外的所有其他月份,即Jan-17 ,2月17日”

When we have a disjoint result set needed (in this case with the closed_date ), we can use the or operator to to achieve this 当我们需要不相交的结果集时(在本例中为closed_date ),我们可以使用or运算符来实现此目标

It is important to enclose the the conditions around closed_date in parenthesis because AND has operator precedence. 重要的是,在条件中将closed_date周围的条件括起来,因为AND具有运算符优先级。

SCOTT@db>with  smple_t as
  2  (        select sysdate - 90 closed_date
  3            from dual
  4           union all
  5          select sysdate - 35 closed_date
  6            from dual
  7           union all
  8          select sysdate + 40 closed_date 
  9          from dual
 10          union all
 11          SELECT SYSDATE - 40 closed_date 
 12          from dual
 13          UNION ALL 
 14          SELECT SYSDATE + 90 closed_date
 15          FROM dual
 16  )
 17  SELECT
 18      closed_date
 19  FROM
 20      smple_t
 21  WHERE
 22      1 = 1
 23      AND   (
 24          closed_date >= add_months(TO_DATE('DEC-2017','MON-YYYY'),1)
 25          OR    closed_date < TO_DATE('DEC-2017','MON-YYYY')
 26      );
CLOSED_DATE               
-----------------------
04-NOV-2017 03:28:17 PM   
14-MAR-2018 03:28:17 PM   
03-MAY-2018 03:28:17 PM   

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

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