Postgres convert time without minutes in format
Postgres convert time without minutes in format
I expected this to be easier than it is, but I have a table with times in it in a format like:
11:35:00
12:02:00
13:04:00
I need to return those times so it only shows:
11:35
12:02
13:04
Every solution I can find online says to use
date_trunc('minute', time_here)
date_trunc('minute', time_here)
That just returns the time in the same seconds format that it is already in. I want the seconds completely removed from the format. Any way to just change it to HH:MM format? Thanks
1 Answer
1
If this is for display purposes, with for example the following definition:
create table t1 (tt timestamp);
insert into t1 values ('2018-08-24 12:10:00');
insert into t1 values ('2018-08-24 11:02:00');
insert into t1 values ('2018-08-24 13:00:30');
The the following:
select to_char(tt, 'HH24:MI') from t1;
will produce:
to_char
12:20
11:02
13:00
date_trunc leaves the data as a timestamp and a select on a timestamp will display in the usual format. Explicit conversion to char (as with to_char) allows different layouts.
Documentation on all different formatting options for this can be found here
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks! For my purpose I only needed to do the "select to_char(tt, 'HH24:MI')" part of the answer, but that works.
– Emac
Aug 24 at 19:52