简体   繁体   English

与Oracle SQL中的日期比较

[英]Comparing with date in Oracle sql

I have a column 'creation_date' which is of type 'date' , when I am querying my table for distinct records based on 'creation_date' I am getting 6 records: 我有一个列'creation_date' ,其类型为'date' ,当我基于'creation_date'查询表中的不同记录时,我得到了6条记录:

select distinct creation_date from test_table;

output: 输出:

06-APR-11
06-APR-11
28-MAR-11
06-APR-11
06-APR-11
18-MAR-11

In this output 6th April is displayed 4 times even when I used distinct in my query. 在此输出中,即使我在查询中使用了distinct,4th 6也显示了4次。 Also when I am trying to find out all records which are matching with creation_date of 6th April 2011 I am not getting any results. 另外,当我尝试查找所有与6th April 2011 creation_date匹配的记录时,也没有任何结果。 Below is my query: 以下是我的查询:

select * from  test_table where creation_date = to_date('06-APR-11','DD-MON-YY');

Please help me where I am doing wrong in these two queries. 请帮助我在这两个查询中做错的地方。

The problem is twofold. 问题是双重的。 Firstly the dates almost definitely have time-components. 首先,日期几乎肯定具有时间成分。 to_date('06-MAR-11','DD-MON-YY') is equivalent to 2011/03/06 00:00:00 . to_date('06-MAR-11','DD-MON-YY')等效于2011/03/06 00:00:00 If you use the TRUNC() function you will be able to see everything for that day: 如果使用TRUNC()函数,您将能够看到当天的所有内容:

select * 
  from test_table
 where trunc(creation_date) = to_date('06-MAR-11','DD-MON-YY');

I would not use the MON datetime format model . 不会使用MON datetime格式模型 As I explain here it depends on your region and settings. 正如我在这里说明的这取决于您所在的地区和设置。 It's safer to use a numeric month format model instead. 相反,使用数字月份格式模型更安全。 Equally, always specify century as part of the year. 同样,始终将世纪指定为一年的一部分。

where trunc(creation_date) = to_date('06-03-YY11','DD-MM-YYYY');

Your second problem is almost definitely your NLS_DATE_FORMAT ; 您的第二个问题几乎肯定是您的NLS_DATE_FORMAT it appears to not take into account the time, hence why you see 4 identical dates. 它似乎没有考虑时间,因此为什么您会看到4个相同的日期。 This only governs the manner in which data is displayed not how it is stored. 这仅控制数据的显示方式,而不是数据的存储方式。

You can change this using something like: 您可以使用以下方式更改此设置:

ALTER SESSION SET NLS_DATE_FORMAT = "DD/MM/YYYY HH24:MI:SS"

If I set up a test environment using the following: 如果我使用以下方法设置测试环境:

create table test_table ( creation_date date );
insert into test_table values ( sysdate );
insert into test_table values ( sysdate - 0.01 );
alter session set nls_date_format = "YYYY/MM/DD";

You can see the data returned does not include time (though SYSDATE does): 您可以看到返回的数据不包含时间(尽管SYSDATE包含):

SQL> select * from test_table;

CREATION_D
----------
2013/04/12
2013/04/12

Altering the NLS_DATE_FORMAT and performing the same SELECT, you now get a time component: 更改NLS_DATE_FORMAT并执行相同的SELECT,现在您将获得一个时间分量:

SQL> alter session set nls_date_format = "YYYY/MM/DD HH24:MI:SS";

Session altered.

SQL> select * from test_table;

CREATION_DATE
-------------------
2013/04/12 12:48:41
2013/04/12 12:34:17

Lastly, when trying to select today's date alone no rows will be returned: 最后,当尝试仅选择今天的日期时,将不返回任何行:

SQL> select *
  2    from test_table
  3   where creation_date = to_date('20130412','yyyymmdd');

no rows selected

But, when using TRUNC() to compare on only the date portion of the field you get all your rows again: 但是,当使用TRUNC()仅对字段的日期部分进行比较时,您将再次获得所有行:

SQL> select *
  2    from test_table
  3   where trunc(creation_date) = to_date('20130412','yyyymmdd');

CREATION_DATE
-------------------
2013/04/12 12:48:41
2013/04/12 12:34:17

To actually answer your second question, if you want unique dates you can re-use the TRUNC() function: 要实际回答第二个问题,如果您想要唯一的日期,则可以重新使用TRUNC()函数:

select distinct trunc(creation_date)
  from test_table

The DATE datatype stores the year (including the century), the month, the day, the hours, the minutes, and the seconds (after midnight). DATE数据类型存储年(包括世纪),月,日,小时,分钟和秒(午夜之后)。 You must also consider this. 您还必须考虑这一点。

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

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