lsankar4033 lsankar4033 - 7 months ago 27
SQL Question

date_part with composite fields in PostgreSQL

Is it possible to use the

date_part
function to extract a composite of multiple specifiers?

For example, I'd like to extract the month, day of month, and hour of day from a timestamp in the single string form
"month:day-of-month:hour-of-day"
so that

`SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');`


would return
"02:16:20"

Answer

This is much easier with to_char():

select to_char(TIMESTAMP '2001-02-16 20:38:40', 'MM:DD:HH24')

The documentation describes the formatting options.

Comments