且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在PostgreSQL中添加可变数量的日期?

更新时间:2023-12-04 22:59:40

由于您要添加小时 ,应该是:

  SELECT date_field + interval'1h'* start_time 

start_time 可以是任何数值。


Apparently, PostgreSQL doesn't have DATEADD, because you can just use the + or - operators.

I need to add a number of hours to a date, which is accomplished like this:

date_field + interval '8.25 hour' 

Which is great, but I need the number of hours to come from a field in the table. Would be something like this:

date_field + interval start_time 'hour' 

I can't find anywhere how to do this. Is this actually impossible?

I don't mind resorting to ugly hacks like taking the field value and dividing by 3600, multiplying by 86400. Whatever I need to do, but I haven't found any way to do that either.

Since you want to add hours, it should rather be:

SELECT date_field + interval '1h' * start_time

start_time can be any numerical value.