creating timestamp from columns in postgres
i have 2 rows in a column, one feeding me the julian date (number of days since Jan 1, 1970, with that day being 1), and the second column is the number of minutes past midnight of the current day (why it was done this way, i have no idea).
i would like to get my sql query to create a timestamp out of these two columns.
if i had access to the data ahead of time, i could do something like SELECT timestamp '1970-01-01 00:00:00' + INTERVAL 'X DAYS' + INTERVAL 'Y MINUTES' AS my_time FROM mytable
, but since X and Y are actual members of the table i'm querying, it's not that simple.
anyone have any suggest开发者_如何转开发ions?
essentially, i'm looking for the equivalent [legal] solution:
SELECT timestamp '1970-01-01 00:00:00' + INTERVAL 'my_col1 DAYS' + INTERVAL 'my_col2 MINUTES' AS my_time FROm mytable
i can get my server-side code to do it for me, but i would love it if i could build it into my query.
You can construct the interval strings and then cast them to the interval
type:
SELECT
timestamp '1970-01-01 00:00:00' +
cast(my_col1 || ' DAYS' AS interval) +
cast(my_col2 || ' MINUTES' AS interval) my_time FROM mytable
Without resorting to string substitution or concatenation:
SELECT
timestamp '1970-01-01 00:00:00' +
my_col1 * interval '1 DAY' +
my_col2 * interval '1 MINUTE' FROM mytable
精彩评论