如何在PostgreSQL中添加可变数量的日期? [英] How to add a variable number of hours to a date in PostgreSQL?
问题描述
显然,PostgreSQL没有 DATEADD
,因为您可以使用 +
或 -
运算符。
我需要添加几个小时到一个日期,这是这样完成的:
date_field + interval'8.25 hour'
哪个是好的,但我需要从表中的一个字段来的小时数。将是这样的:
date_field + interval start_time'hour'
pre>
我找不到任何地方如何做到这一点。这实际上是不可能的吗?
我不介意使用丑陋的黑客,如采取场地价值,除以3600,乘以86400.无论我需要做什么,但是我没有找到任何方法。
解决方案由于您要添加小时 ,应该是:
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.这篇关于如何在PostgreSQL中添加可变数量的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!