从时间戳列中选择日期范围

发布于 2024-12-04 18:25:57 字数 705 浏览 0 评论 0原文

目前我的表有一列用于存储日期和时间。该列的数据类型是无时区的时间戳。因此它的值格式为 '2011-09-13 11:03:44.537'

我需要检索有关日期的行。如果我使用:

select * from table where mdate >= '2011-09-13 11:03:44.537'
                      and mdate <= '2011-09-12 11:03:44.537'

它将提供“2011-09-13 11:03:44.537”和“2011-09-12 11:03:44.537”之间的值。

但如果我要使用:

select * from table where mdate >= '2011-09-13 00:00:00.0'
                      and mdate <= '2011-09-12 00:00:00.0'

没有日期、月份和秒,它不会显示任何行。

如何从此表中获取与日期相关的值(仅包含日期,忽略小时、分钟和秒)?

即使该列有带时间戳的日期,我也只需要使用日期来搜索它们(忽略时间戳或将小时、分钟和秒设置为 0 例如 '2011-09-13 00: 00:00.0')。

Currently my table has a column to store date and time. The data type of that column is timestamp without time zone. So it has values in the format '2011-09-13 11:03:44.537'.

I need to retrieve rows with respect to the date. If I use:

select * from table where mdate >= '2011-09-13 11:03:44.537'
                      and mdate <= '2011-09-12 11:03:44.537'

it will provide the values which are in between '2011-09-13 11:03:44.537' and '2011-09-12 11:03:44.537'.

But if I am going with:

select * from table where mdate >= '2011-09-13 00:00:00.0'
                      and mdate <= '2011-09-12 00:00:00.0'

without date, month and seconds, It is not displaying any rows.

How to fetch values from this table with respect to the date (only with date, ignoring hour, minutes and seconds)?

Even, the column has date with timestamp, I need to search them only with date (ignoring timestamp or making the hour, minutes and seconds to 0 such as '2011-09-13 00:00:00.0').

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍笑 2024-12-11 18:25:57

如果您要将字段的 timestamp 值转换为 date(如另一个答案中所建议的那样),性能将会下降,因为列中的每个值都需要转换为不能使用比较和简单索引。您必须在表达式上创建一个特殊的 索引,如下所示:

CREATE INDEX some_idx ON tbl (cast(mdate AS date));

在这种情况下,查询也应该简化为:

SELECT * FROM tbl
WHERE  mdate::date = '2011-09-12'::date; -- 2nd cast optional

但是,据我所知,您的问题是查询中的一个简单的拼写错误/思考:
切换了上层&下限。尝试:

SELECT * FROM tbl
WHERE  mdate >= '2011-09-12 00:00:00.0'
AND    mdate <= '2011-09-13 00:00:00.0';

或者,为了简化语法并更精确:

SELECT * FROM tbl
WHERE  mdate >= '2011-09-12 00:00'
AND    mdate <  '2011-09-13 00:00';
  • 无需拼写秒和 0 的分数。
  • 您不想包含 2011-09-13 00:00,因此请使用 < 而不是 <=
    出于同样的原因,请勿在此处使用 BETWEEN
WHERE mdate BETWEEN '2011-09-12 00:00' AND '2011-09-13 00:00'

这将包括第二天的 00:00。

另请注意,timestamp [without time zone] 类型的列将根据您当前的时区设置进行解释。日期部分取决于该设置,通常应按预期工作。更多详细信息:

If you are going to cast the timestamp values of the field to date, as suggested in another answer, performance will degrade because every single value in the column needs to be cast for comparison and simple indexes cannot be used. You would have to create a special index on the expression, like so:

CREATE INDEX some_idx ON tbl (cast(mdate AS date));

In this case the query should also be simplified to:

SELECT * FROM tbl
WHERE  mdate::date = '2011-09-12'::date; -- 2nd cast optional

However, as far as I can see, your problem is a simple typo / thinko in your query:
You switched upper & lower boundaries. Try:

SELECT * FROM tbl
WHERE  mdate >= '2011-09-12 00:00:00.0'
AND    mdate <= '2011-09-13 00:00:00.0';

Or, to simplify your syntax and be more precise:

SELECT * FROM tbl
WHERE  mdate >= '2011-09-12 00:00'
AND    mdate <  '2011-09-13 00:00';
  • There is no need to spell out seconds and fractions that are 0.
  • You don't want to include 2011-09-13 00:00, so use < instead of <=.
    Don't use BETWEEN here, for the same reason:
WHERE mdate BETWEEN '2011-09-12 00:00' AND '2011-09-13 00:00'

This would include 00:00 of the next day.

Also be aware that a column of type timestamp [without time zone] is interpreted according to your current time zone setting. The date part depends on that setting, which should generally work as expected. More details:

冷心人i 2024-12-11 18:25:57

只需将时间戳视为日期:

select * 
from table 
where mdate::date >= DATE '2011-09-12' 
  and mdate::date <= DATE '2011-09-13'

表达式 mdate::date 会将时间戳转换为 date 类型,这将从值中删除时间部分。

DATE '2011-09-13' 是一个(标准)DATE 文字,它比简单地编写“2011-09-13”更健壮,因为它不受任何语言设置的影响。

Just treat the timestamp as a date:

select * 
from table 
where mdate::date >= DATE '2011-09-12' 
  and mdate::date <= DATE '2011-09-13'

The expression mdate::date will cast the timestamp to a date type which will remove the time part from the value.

DATE '2011-09-13' is a (standard) DATE literal which is a bit more robust than simply writing '2011-09-13' as it isn't affected by any language settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文