Extraer fecha(aaaa/mm / dd) de una marca de tiempo en PostgreSQL


Quiero extraer solo la parte de fecha de una marca de tiempo en PostgreSQL.

Necesito que sea de tipo postgresql DATE para poder insertarlo en otra tabla que espere un valor DATE.

Por ejemplo, si tengo 2011/05/26 09:00:00, quiero 2011/05/26

Traté de casting, pero sólo tengo 2011:

timestamp:date
cast(timestamp as date)

He intentado to_char() con to_date():

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

Traté de hacerlo una función:

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

¿Cuál es la mejor manera de extraer la fecha (aaaa/mm / dd) desde una marca de tiempo en PostgreSQL?

Author: Eric Leschinski, 2011-05-26

6 answers

Puede convertir su marca de tiempo a una fecha mediante el sufijo ::date. Aquí, en psql, hay una marca de tiempo:

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

Ahora lo lanzaremos a una fecha:

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

Por otro lado, puede usar la función date_trunc. La diferencia entre ellos es que este último devuelve el mismo tipo de datos como timestamptz manteniendo su zona horaria intacta (si lo necesita).

=> select date_trunc('day', now());
       date_trunc
------------------------
 2015-12-15 00:00:00+02
(1 row)
 318
Author: Wayne Conrad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-15 16:10:59

Utilice la función date :

select date(timestamp_field) from table

Desde una representación de campo de caracteres hasta una fecha puede usar:

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

Código de prueba:

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;

Resultado de la prueba:

Resultado de pgAdmin

pgAdmin resultado ancho

 77
Author: jamesallman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-26 03:49:48

¿Has tratado de darle una fecha, con <mydatetime>::date?

 7
Author: leonbloy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-26 02:39:47

Esto funciona para mí en python 2.7

 select some_date::DATE from some_table;
 7
Author: thedarkgriffen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-12 07:11:42
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)

Otro kit de prueba:

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

SHOW datestyle ;
 DateStyle 
-----------
 ISO, MDY
(1 row)
 2
Author: Grzegorz Szpetkowski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-26 02:57:11

Simplemente haga select date(timestamp_column) y obtendrá la única parte de la fecha. A veces hacer select timestamp_column::date puede devolver date 00:00:00 donde no elimina la parte 00:00:00. Pero he visto date(timestamp_column) funcionar perfectamente en todos los casos. Espero que esto ayude.

 0
Author: Koushik Das,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-17 09:42:34