如何比较日期在Postgresql中的日期字段? [英] How to compare dates in datetime fields in Postgresql?

查看:4607
本文介绍了如何比较日期在Postgresql中的日期字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在面对一个奇怪的情况,当比较postgresql中的日期(在windows中的版本9.2.4)。我有一个列在我的表说update_date类型'时间戳没有时区'。客户可以在此字段中搜索,只包含日期(即:2013-05-03)或具有时间的日期(即:2013-05-03 12:20:00)。此列具有当前所有行的时间戳值,并具有相同的日期部分(2013-05-03),但时间差异部分。

I have been facing a strange scenario when comparing between dates in postgresql(version 9.2.4 in windows). I have a column in my table say update_date with type 'timestamp without timezone'. Client can search over this field with only date (i.e: 2013-05-03) or date with time (i.e: 2013-05-03 12:20:00). This column has the value as timestamp for all rows currently and have the same date part(2013-05-03) but difference in time part.

在这一列,我得到不同的结果。如下所示:

When I'm comparing over this column, I'm getting different results. Like the followings:

select * from table where update_date >= '2013-05-03' AND update_date <= '2013-05-03' -> No results

select * from table where update_date >= '2013-05-03' AND update_date < '2013-05-03' -> No results

select * from table where update_date >= '2013-05-03' AND update_date <= '2013-05-04' -> results found

select * from table where update_date >= '2013-05-03' -> results found

我的问题是如何让第一个查询可能获得结果,我的意思是为什么第三个查询是否正常,但不是第一个查询?

My question is how can I make the first query possible to get results, I mean why the 3rd query is working but not the first one?

任何人都可以帮我吗?提前感谢。

Can anybody help me with this? Thanks in advance.

推荐答案

@Nicolai对于转换是正确的,为什么条件对于任何数据为假。我想你更喜欢第一种形式,因为你想避免日期操作输入字符串,正确吗?您不需要害怕:

@Nicolai is correct about casting and why the condition is false for any data. i guess you prefer the first form because you want to avoid date manipulation on the input string, correct? you don't need to be afraid:

SELECT *
FROM table
WHERE update_date >= '2013-05-03'::date
AND update_date < ('2013-05-03'::date + '1 day'::interval);

这篇关于如何比较日期在Postgresql中的日期字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆