Oracle-SQL 中没有时间值的日期总是 00:00:00 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3333075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is a date in Oracle-SQL without a time-value always 00:00:00?
提问by Husky110
Heyho,
嘿嘿,
I need to grab some datas from actions which been done from date A 00:00:00 and date B 00:00:00 (in this case Date A: 16.07.2010 Date B: 20.07.2010) so i wrote this select-statement:
我需要从日期 A 00:00:00 和日期 B 00:00:00(在这种情况下日期 A:16.07.2010 日期 B:20.07.2010)开始的操作中获取一些数据,所以我写了这个选择-陈述:
Select avg(cnt),fext from (
Select
to_char(mytable.dateadded, 'DD.MM.YYYY') dateadded,
fext,
count(id) cnt
from mytable
where dateadded between
to_date('16.07.2010', 'dd,MM,YYYY') and
to_date('20.07.2010', 'dd,MM,YYYY')
group by
to_char(mytable.dateadded, 'DD.MM.YYYY'),
fext)
group by fext;
The original (and working) statement had:
原始(和工作)声明有:
to_date('16.07.2010 00:00:00', 'dd,MM,YYYY HH24:Mi:SS') and
to_date('20.07.2010 00:00:00', 'dd,MM,YYYY HH24:Mi:SS')
so the question is: does the
所以问题是:
to_date('16.07.2010', 'dd,MM,YYYY') and
to_date('20.07.2010', 'dd,MM,YYYY')
already set the time to date A and B to 00:00:00?
已经将日期 A 和 B 的时间设置为 00:00:00?
Greetz
格雷茨
回答by Michael Pakhantsov
If you does not specify time part of date it will be 00:00:00.
如果您没有指定日期的时间部分,它将是 00:00:00。
If you worry about time part you always can truncate time part:
如果您担心时间部分,您总是可以截断时间部分:
Trunc(to_date('16.07.2010', 'dd.MM.YYYY')) and
Trunc(to_date('20.07.2010', 'dd.MM.YYYY'))
回答by Rob van Wijk
The easiest way is to use ANSI date literals, which doesn't allow time to be specified and is thus with a 00:00:00 time part internally:
最简单的方法是使用 ANSI 日期文字,它不允许指定时间,因此在内部使用 00:00:00 时间部分:
dateadded between date '2010-07-16' and date '2010-07-20'
Your expressions look odd, as your date string has dots as the time component separator, but your date format has commas. Oracle accepts it though:
您的表达式看起来很奇怪,因为您的日期字符串有点作为时间组件分隔符,但您的日期格式有逗号。Oracle 接受它:
rwijk@XE> select to_date('16.07.2010', 'dd,MM,YYYY') from dual;
TO_DATE('16.07.2010
-------------------
16-07-2010 00:00:00
1 row selected.
I'd use the ANSI date literal for its simplicity.
为了简单起见,我会使用 ANSI 日期文字。
Regards, Rob.
问候,罗布。
回答by mcha
this query will returns you one row which answers your question i think :
这个查询将返回你的一行来回答你我认为的问题:
SELECT TO_DATE('16.07.2010 00:00:00', 'dd,MM,YYYY HH24:Mi:SS'),
TO_DATE('16.07.2010', 'dd,MM,YYYY')
FROM dual
WHERE to_date('16.07.2010 00:00:00', 'dd,MM,YYYY HH24:Mi:SS') = to_date('16.07.2010', 'dd,MM,YYYY')
回答by OMG Ponies
This is one thing that SQL is consistent about - if you don't provide a time portion on a DATE data type that includes a time portion (IE Oracle, PostgreSQL while MySQL and SQL Server explicitly call it DATETIME), then the time portion is defaulting to exactly midnight of that date - 00:00:00. And being midnight, that means the start of the date - if you want to include the entire date in a date comparison, the value's time portion needs to be 23:49:49 -- a second to midnight.
这是 SQL 一致的一件事 - 如果您没有在包含时间部分的 DATE 数据类型上提供时间部分(IE Oracle、PostgreSQL,而 MySQL 和 SQL Server 明确称其为 DATETIME),则时间部分是默认为该日期的午夜 - 00:00:00。并且是午夜,这意味着日期的开始——如果你想在日期比较中包括整个日期,值的时间部分需要是 23:49:49——一秒到午夜。
回答by John Lilley
23:59:59 is a second to midnight. Essentially midnight is defined as the start of a day: 00:00:00. Then the seconds clock up to the end of the day.
23:59:59 是午夜的一秒。基本上午夜被定义为一天的开始:00:00:00。然后秒针计时到一天结束。