Select timestamp with time zone, but ignore seconds
Asked Answered
C

3

14

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?

Cabotage answered 2/11, 2015 at 23:14 Comment(0)
G
28

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') ...

This formats the timestamptz value according to your current timezone setting. See:

Gothurd answered 2/11, 2015 at 23:30 Comment(0)
C
0

With timezone:

SELECT date_trunc('minute', TABLE_NAME.COLUMN AT TIME ZONE '+03') FROM TABLE_NAME
Curiel answered 13/10, 2023 at 6:57 Comment(0)
G
0

Well, there are many ways to handle this, but the efficient way is to use date_trunc, as mentioned in the privous answer.

An alternative pproach is to use to_char function.

Example

SELECT TO_CHAR(timestamp_column, 'YYYY-MM-DD HH24:MI') AS formatted_ts
FROM table_name;
Grocer answered 13/10, 2023 at 7:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.