SQL 从 PostgreSQL 的时间戳中提取日期 (yyyy/mm/dd)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6133107/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:46:13  来源:igfitidea点击:

Extract date (yyyy/mm/dd) from a timestamp in PostgreSQL

sqlpostgresqlcastingtimestamp

提问by keren

I want to extract just the date part from a timestamp in PostgreSQL.

我只想从 PostgreSQL 的时间戳中提取日期部分。

I need it to be a postgresql DATEtype so I can insert it into another table that expects a DATEvalue.

我需要它是 postgresqlDATE类型,以便我可以将它插入到另一个需要DATE值的表中。

For example, if I have 2011/05/26 09:00:00, I want 2011/05/26

例如,如果我有2011/05/26 09:00:00,我想要2011/05/26

I tried casting, but I only get 2011:

我试过铸造,但我只得到 2011 年:

timestamp:date
cast(timestamp as date)

I tried to_char()with to_date():

我试着to_char()to_date()

SELECT to_date(to_char(timestamp, 'YYYY/MM/DD'), 'YYYY/MM/DD') 
FROM val3 WHERE id=1;

I tried to make it a function:

我试图使它成为一个函数:

CREATE OR REPLACE FUNCTION testing() RETURNS void AS '
DECLARE i_date DATE;
BEGIN
    SELECT to_date(to_char(val1, "YYYY/MM/DD"),"YYYY/MM/DD") 
      INTO i_date FROM exampTable WHERE id=1;
    INSERT INTO foo(testd) VALUES (i);
END

What is the best way to extract date (yyyy/mm/dd) from a timestamp in PostgreSQL?

从 PostgreSQL 的时间戳中提取日期 (yyyy/mm/dd) 的最佳方法是什么?

回答by Wayne Conrad

You can cast your timestamp to a date by suffixing it with ::date. Here, in psql, is a timestamp:

您可以通过添加后缀将时间戳转换为日期::date。这里,在 psql 中,是一个时间戳:

# select '2010-01-01 12:00:00'::timestamp;
      timestamp      
---------------------
 2010-01-01 12:00:00

Now we'll cast it to a date:

现在我们将它投射到一个日期:

wconrad=# select '2010-01-01 12:00:00'::timestamp::date;
    date    
------------
 2010-01-01

On the other hand you can use date_truncfunction. The difference between them is that the latter returns the same data type like timestamptzkeeping your time zone intact (if you need it).

另一方面,您可以使用date_trunc函数。它们之间的区别在于后者返回相同的数据类型,例如timestamptz保持时区完整(如果需要)。

=> select date_trunc('day', now());
       date_trunc
------------------------
 2015-12-15 00:00:00+02
(1 row)

回答by James Allman

Use the datefunction:

使用日期函数:

select date(timestamp_field) from table

From a character field representation to a date you can use:

从字符字段表示到日期,您可以使用:

select date(substring('2011/05/26 09:00:00' from 1 for 10));

Test code:

测试代码:

create table test_table (timestamp_field timestamp);
insert into test_table (timestamp_field) values(current_timestamp);
select timestamp_field, date(timestamp_field) from test_table;

Test result:

测试结果:

pgAdmin result

pgAdmin 结果

pgAdmin result wide

pgAdmin 结果广泛

回答by leonbloy

Have you tried to cast it to a date, with <mydatetime>::date?

您是否尝试将其转换为日期<mydatetime>::date

回答by thedarkgriffen

This works for me in python 2.7

这在 python 2.7 中对我有用

 select some_date::DATE from some_table;

回答by Koushik Das

Just do select date(timestamp_column)and you would get the only the date part. Sometimes doing select timestamp_column::datemay return date 00:00:00where it doesn't remove the 00:00:00part. But I have seen date(timestamp_column)to work perfectly in all the cases. Hope this helps.

只要这样做select date(timestamp_column),你就会得到唯一的日期部分。有时这样做select timestamp_column::date可能会返回date 00:00:00它没有移除00:00:00零件的地方。但我已经看到date(timestamp_column)在所有情况下都能完美地工作。希望这可以帮助。

回答by Grzegorz Szpetkowski

CREATE TABLE sometable (t TIMESTAMP, d DATE);
INSERT INTO sometable SELECT '2011/05/26 09:00:00';
UPDATE sometable SET d = t; -- OK
-- UPDATE sometable SET d = t::date; OK
-- UPDATE sometable SET d = CAST (t AS date); OK
-- UPDATE sometable SET d = date(t); OK
SELECT * FROM sometable ;
          t          |     d      
---------------------+------------
 2011-05-26 09:00:00 | 2011-05-26
(1 row)

Another test kit:

另一个测试套件:

SELECT pg_catalog.date(t) FROM sometable;
    date    
------------
 2011-05-26
(1 row)

SHOW datestyle ;
 DateStyle 
-----------
 ISO, MDY
(1 row)

回答by Elmira Behzad

In postgres simply : TO_CHAR(timestamp_column, 'DD/MM/YYYY') as submission_date

在 postgres 中简单地:TO_CHAR(timestamp_column, 'DD/MM/YYYY') 作为 submit_date