Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql select timestamp with time zone, but ignore seconds

I'm selecting a timestamptz from a PosgreSQL database.

I want my SELECT to return only the date, hours and minutes. No seconds. Can I set the date format in my psql SELECT to accommodate this?

like image 726
Ben Walker Avatar asked Nov 02 '15 23:11

Ben Walker


People also ask

How does Postgres store timestamp with timezone?

In PostgreSQL 2 temporal data types namely timestamp and timestamptz where one is without timezone and the later is with timezone respectively, are supported to store Time and Date to a column. Both timestamp and timestamptz uses 8 bytes for storing timestamp values.

Does Postgres timestamp have timezone?

Introduction to PostgreSQL timestamp The timestamp datatype allows you to store both date and time. However, it does not have any time zone data. It means that when you change the timezone of your database server, the timestamp value stored in the database will not change automatically.

How does timestamp with timezone work?

For timestamp with time zone , the internally stored value is always in UTC (Universal Coordinated Time, traditionally known as Greenwich Mean Time, GMT ). An input value that has an explicit time zone specified is converted to UTC using the appropriate offset for that time zone.


1 Answers

You can use date_trunc() to truncate seconds and still return a timestamptz:

SELECT date_trunc('minute', ts_col) ...

Or you can use to_char() to return a formatted timestamp as text any way you like:

SELECT to_char(ts_col, 'YYYY-MM-DD HH24:MI') ...

Note that this will format the timestamptz according to your current time zone setting. Details:

  • Ignoring time zones altogether in Rails and PostgreSQL
like image 65
Erwin Brandstetter Avatar answered Sep 23 '22 21:09

Erwin Brandstetter