从 PostgreSQL 中的时间戳获取日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7322969/
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
getting date from timestamp in PostgreSQL
提问by daydreamer
I have a PostgreSQL timestamp as
我有一个 PostgreSQL 时间戳作为
2009-12-22 11:01:46
I need to change this to date as
我需要将其更改为日期
2009-12-22
So that I can compare the dates in postgreSQL
这样我就可以比较日期 postgreSQL
How can I achieve this transformation?
我怎样才能实现这种转变?
回答by plundra
Cast it to date
.
将其投射到date
.
SELECT yourtimestamp::date;
If you need to extract other kinds of stuff, you might want to use EXTRACTor date_trunc
如果您需要提取其他类型的东西,您可能需要使用EXTRACT或date_trunc
Both links are to the same page, were you'll find more date/time-related functions.
如果您会发现更多与日期/时间相关的功能,那么这两个链接都指向同一页面。
回答by DNS
You can either use one of the Postgres date functions, such as date_trunc, or you could just cast it, like this:
您可以使用 Postgres日期函数之一,例如 date_trunc,或者您可以直接转换它,如下所示:
SELECT timestamp '2009-12-22 11:01:46'::date
>>> 2009-12-22