oracle中的日期函数
我有一个关于 oracle 上的日期函数的问题。
我有下表
statistic_table(
pages AS varchar(10),
date_created AS date
);
我有以下 sql
SELECT COUNT(*) FROM statistic_table WHERE date_created BETWEEN sysdate-5 AND sysdate-1
and
SELECT COUNT(*) FROM statistic_table WHERE date_created BETWEEN to_date('12-AUG-2011') AND to_date('16-AUG-2011');
问题是,为什么它返回不同的数字。假设 sysdate-5 返回 12-aug-2011 并且 sysdate-1 返回 16-aug-2011
任何帮助将不胜感激!
干杯,
I've got a question about date function on oracle.
I have the following table
statistic_table(
pages AS varchar(10),
date_created AS date
);
I have the following sql
SELECT COUNT(*) FROM statistic_table WHERE date_created BETWEEN sysdate-5 AND sysdate-1
and
SELECT COUNT(*) FROM statistic_table WHERE date_created BETWEEN to_date('12-AUG-2011') AND to_date('16-AUG-2011');
the question is, why is it return different numbers. assuming sysdate-5 returns 12-aug-2011 and sysdate-1 returns 16-aug-2011
Any help would be much appreciated!
Cheers,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
sysdate - 5 将为您提供当前时间的日期。因此,如果我在下午 1 点精确地运行它,则查询将相当于:
而第二个查询是:
您应该尝试这样做:
sysdate - 5 will give you a date with the current time. So if I ran it at 1pm precisely, the query would be equivalent to:
whereas the second query is:
you should probably try this instead:
Oracle 中的日期是精确到秒的时间点。
SYSDATE
返回当前日期和时间,因此与to_date('17-AUG-2011')
不同:使用
TRUNC
函数,如果您只需要日期组件:A date in Oracle is a point in time with a precision of a second.
SYSDATE
returns the current date and time and is therefore not the same asto_date('17-AUG-2011')
:Use the
TRUNC
function if you only want the date component:因为 SYSDATE 包含时间分量,所以如果当前时间是 11:22:33,那么
实际上相当于要
避免时间分量,请执行以下操作:
Because SYSDATE includes a time component, so if the current time is 11:22:33, then
is actually equivalent to
To avoid the time component do this:
Oracle
DATE
始终包含日期和时间部分。sysdate-5
返回 5 天前的日期。例如,如果今天是 8 月 17 日上午 10 点,sysdate-5
将返回 8 月 12 日上午 10 点。另一方面,
to_date('12-AUG-2011', 'DD-MON-YYYY')
返回 8 月 12 日午夜。因此它返回的日期比sysdate-5
早 10 小时。An Oracle
DATE
always has a day and a time component.sysdate-5
returns a date exactly 5 days ago. If today is August 17 at 10 AM, for example,sysdate-5
returns August 12 at 10 AM.to_date('12-AUG-2011', 'DD-MON-YYYY')
, on the other hand, returns August 12 at midnight. So it returns a date that is 10 hours earlier thansysdate-5
.sysdate 自动返回,并带有前面答案中提到的时间组件。
使用 to_date 时,它将字符串转换为日期。话虽如此,您可以传入参数以使其返回相同的内容。
看看这个解释它的链接。
to_date 参数
sysdate auto returns with a time component as mentioned by the previous answers.
When using to_date it is converting a string to a date. With this being said you can pass in parameters to make it return the same thing.
Have a look at this link that explains it.
to_date parameters