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

发布于 2024-11-09 15:15:07 字数 851 浏览 0 评论 0原文

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

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

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

我尝试投射,但只得到 2011 :

timestamp:date
cast(timestamp as date)

我尝试使用 to_char()to_date()

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

我尝试将其设为函数:

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

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

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

I need it to be a postgresql DATE type so I can insert it into another table that expects a DATE value.

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

I tried casting, but I only get 2011:

timestamp:date
cast(timestamp as date)

I tried to_char() with 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?

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

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

发布评论

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

评论(8

带刺的爱情 2024-11-16 15:15:07

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

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

现在我们将其转换为日期:

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

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

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

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

# 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_trunc function. The difference between them is that the latter returns the same data type like timestamptz keeping your time zone intact (if you need it).

=> select date_trunc('day', now());
       date_trunc
------------------------
 2015-12-15 00:00:00+02
(1 row)
肥爪爪 2024-11-16 15:15:07

使用 date 函数:

select date(timestamp_field) from table

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

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

测试代码:

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;

测试结果:

pgAdmin result

pgAdmin 结果宽

Use the date function:

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 result wide

喜你已久 2024-11-16 15:15:07

在 postgres 中简单地说:

TO_CHAR(timestamp_column, 'DD/MM/YYYY') as submission_date

In postgres simply :

TO_CHAR(timestamp_column, 'DD/MM/YYYY') as submission_date
瞳孔里扚悲伤 2024-11-16 15:15:07

您是否尝试过使用 ::date 将其转换为日期?

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

贱人配狗天长地久 2024-11-16 15:15:07

只需select date(timestamp_column),您就会得到唯一的日期部分。
有时,select timestamp_column::date 可能会返回 date 00:00:00,但不会删除 00:00:00 部分。但我已经看到 date(timestamp_column) 在所有情况下都能完美工作。希望这有帮助。

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

苦妄 2024-11-16 15:15:07

这在 python 2.7 中对我有用

 select some_date::DATE from some_table;

This works for me in python 2.7

 select some_date::DATE from some_table;
心碎无痕… 2024-11-16 15:15:07
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)

另一个测试套件:

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

SHOW datestyle ;
 DateStyle 
-----------
 ISO, MDY
(1 row)
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)
娇柔作态 2024-11-16 15:15:07

您可以使用date_trunc('day', field)

select date_trunc('day', data_gps) as date_description from some_table;

You can use date_trunc('day', field).

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