Home > OS >  How do you make a timestamp from separate date and time columns in Postgresql?
How do you make a timestamp from separate date and time columns in Postgresql?

Time:03-18

I have a date column and a time column in Postgresql. I would like to combine the two columns into a timestamp column. For example, I want

date_col (timestamp)
-----------------------
2021-06-28 00:00:00.000
2021-10-11 00:00:00.000
2021-04-01 00:00:00.000

time_col (varchar)
-----------------------
'01:25:00'
'10:55:00'
'08:25:00'

to become

timestamp_col (timestamp)
-----------------------
2021-06-28 01:25:00
2021-10-11 10:55:00
2021-04-01 08:25:00

Is this possible without tediously doing

make_timestamp(date_part('year', date_col), date_part('month', date_col), date_part('day', date_col), date_part('hour', time_col), date_part('minute', time_col), date_part('second', time_col))

CodePudding user response:

Just cast the timestamp to a date and add the time (after casting that to a time):

select date_col::date   time_col::time as timestamp_col
from the_table;
  • Related