1

我有一个结构表:

+---------+-------------+------+------ ---------+--------+------+
| week_no | long_week_no | week_start_date | 周末结束日期 | 月 | 年份 |
+---------+-------------+------+------ ---------+--------+------+
| 1 | 1A | 2015 年 1 月 1 日 | 2015 年 3 月 1 日 | 一月 | 2015 |
| 1 | 1B | 2015 年 4 月 1 日 | 2015 年 10 月 1 日 | 一月 | 2015 |
| 2 | 2 | 2015 年 11 月 1 日 | 17/01/2015 | 一月 | 2015 |
| 3 | 3 | 18/01/2015 | 24/01/2015 | 一月 | 2015 |
| .. | .. | .. | .. | .. | .. |
| 51 | 51 | 2014 年 12 月 14 日 | 2015 年 12 月 20 日 | 十二月 | 2014 |
+---------+-------------+------+------ ---------+--------+------+

当我运行以下语句时:

SELECT * 
FROM   loy_period 
WHERE  To_date('15/04/2015', 'DD/MM/YYYY') BETWEEN 
       To_date(week_start_date, 'DD/MM/YYYY') AND 
       To_date(week_end_date, 'DD/MM/YYYY'); 

SELECT * 
FROM   loy_period 
WHERE  To_date('15/04/2015', 'DD/MM/YYYY') BETWEEN 
       week_start_date AND 
       week_end_date; 

它返回以下内容:

+---------+-------------+------+------ ---------+--------+------+
| week_no | long_week_no | week_start_date | 周末结束日期 | 月 | 年份 |
+---------+-------------+------+------ ---------+--------+------+
| 51 | 51 | 2014 年 12 月 14 日 | 2015 年 12 月 20 日 | 十二月 | 2014 |
| 1 | 1A | 2015 年 1 月 1 日 | 2015 年 3 月 1 日 | 一月 | 2015 |
+---------+-------------+------+------ ---------+--------+------+

当我使用任何日期运行它时,除了week_no 51 记录之外,它还会返回正确的期间!

我很困惑为什么会这样。列week_start_dateweek_end_date类型date

小提琴按预期工作。

4

1 回答 1

0

正确使用TO_DATE和 DATE 值对我来说非常有效。

  • 切勿在DATE上使用TO_DATE,它会隐式将其转换为字符串,然后使用特定于语言环境的 NLS 格式返回日期

  • '01/01/2015'不是日期,而是字符串。您必须使用TO_DATE将其显式转换为 DATE。

走着瞧吧:

SQL> explain plan for select * from dual where to_date(sysdate) > to_date(sysdate -1);

Explained.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 3752461848

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |     2 |     2   (0)| 00:00:01 |
|*  1 |  FILTER            |      |       |       |            |          |
|   2 |   TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
---------------------------------------------------

   1 - filter(TO_DATE(TO_CHAR(SYSDATE@!))>TO_DATE(TO_CHAR(SYSDATE@!-1)))

14 rows selected.

SQL>

因此,实际应用的过滤器是filter(TO_DATE(TO_CHAR(SYSDATE@!))由于基于特定语言环境的 NLS 格式的隐式转换,您将得到不正确的输出。

无论如何,现在回到你原来的问题。

例如,

假设您的数据如下所示:

设置:

SQL> CREATE TABLE t
  2      (week_no VARCHAR2(2), long_week_no VARCHAR2(2), week_start_date DATE, week_end_date DATE, mnth VARCHAR2(3), yr VARCHAR2(4))
  3  ;

Table created.

SQL>
SQL>
SQL> INSERT ALL
  2      INTO t (week_no, long_week_no, week_start_date, week_end_date, mnth, yr)
  3           VALUES ('1', '1A', TO_DATE('01/01/2015','DD/MM/YYYY'), TO_DATE('03/01/2015','DD/MM/YYYY'), 'JAN', '2015')
  4      INTO t (week_no, long_week_no, week_start_date, week_end_date, mnth, yr)
  5           VALUES ('1', '1B', TO_DATE('04/01/2015','DD/MM/YYYY'), TO_DATE('10/01/2015','DD/MM/YYYY'), 'JAN', '2015')
  6      INTO t (week_no, long_week_no, week_start_date, week_end_date, mnth, yr)
  7           VALUES ('2', '2', TO_DATE('11/01/2015','DD/MM/YYYY'), TO_DATE('17/01/2015','DD/MM/YYYY'), 'JAN', '2015')
  8      INTO t (week_no, long_week_no, week_start_date, week_end_date, mnth, yr)
  9           VALUES ('3', '3', TO_DATE('18/01/2015','DD/MM/YYYY'), TO_DATE('24/01/2015','DD/MM/YYYY'), 'JAN', '2015')
 10      INTO t (week_no, long_week_no, week_start_date, week_end_date, mnth, yr)
 11           VALUES ('51', '51', TO_DATE('20/12/2014','DD/MM/YYYY'), TO_DATE('26/12/2015','DD/MM/YYYY'), 'DEC', '2014')
 12  SELECT * FROM dual
 13  ;

5 rows created.

SQL>
SQL> COMMIT;

Commit complete.

SQL>

桌子:

SQL> SELECT * FROM t;

WE LO WEEK_STAR WEEK_END_ MNT YR
-- -- --------- --------- --- ----
1  1A 01-JAN-15 03-JAN-15 JAN 2015
1  1B 04-JAN-15 10-JAN-15 JAN 2015
2  2  11-JAN-15 17-JAN-15 JAN 2015
3  3  18-JAN-15 24-JAN-15 JAN 2015
51 51 20-DEC-14 26-DEC-15 DEC 2014

SQL>

查询以根据 DATE 范围过滤行:

SQL> SELECT *
  2  FROM   t
  3  WHERE  To_date('15/01/2015', 'DD/MM/YYYY') BETWEEN
  4         week_start_date AND
  5         week_end_date;

WE LO WEEK_STAR WEEK_END_ MNT YR
-- -- --------- --------- --- ----
2  2  11-JAN-15 17-JAN-15 JAN 2015
51 51 20-DEC-14 26-DEC-15 DEC 2014

SQL>
于 2015-04-10T10:53:22.847 回答