我有时间戳,我想修改这个时间戳,以便在下一个早上6点创建新的时间戳。例如
2020-09-01 22:00:00 -> 2020-09-02 06:00:00
2020-08-30 04:00:00 -> 2020-08-30 06:00:00
你知道一步就能做到吗?
目前,我有伪代码,例如:
hour = extract(hour from mytimestamp)
if hour > 0 and hour < 7 then
date_trunc('hour', mytimestamp) + interval 6-hour
else
date_trunc('day', mytimestamp) + interval '1 day' + interval '6 hours'
end if;
发布于 2020-09-01 18:43:26
您可以使用一个案例表达式来实现这一点:
select case
when the_column::time > time '07:00' then (the_column::date + 1) + time '06:00'
else the_column::date + time '06:00'
end as new_column
from the_table
the_column::date
将时间戳转换为日期(没有时间)。然后,只需将所需的时间添加到该日期,即可获得新的时间戳。为了获得第二天,使用+1
。
https://stackoverflow.com/questions/63693580
复制相似问题