简体   繁体   English

如何将 postgres 的列日期更新到提前几天

[英]How to update a column date of postgres to some days ahead

Im using postgres as my database and I have a table that has a date column.我使用 postgres 作为我的数据库,并且我有一个包含日期列的表。 The current value in this column is 2021-04-1 17:19:08此列中的当前值为 2021-04-1 17:19:08

I want to update this column's value and move the date 20 (or any other amount of days) ahead so the new value will be 2021-04-20 17:19:08我想更新此列的值并将日期提前 20(或任何其他天数),因此新值将为 2021-04-20 17:19:08

the reason Im not doing it manually its because I have too much rows to move ahead so I need a query for that, and I need to freedom to choose the amount of days ahead我没有手动执行的原因是因为我有太多的行要向前移动所以我需要一个查询,我需要自由选择提前的天数

Thanks in advance提前致谢

You can add days using INTERVAL -您可以使用INTERVAL添加天数 -

SELECT CURRENT_DATE + INTERVAL '20 days';

With the help of Nikhil Patil I managed to create the next query:在 Nikhil Patil 的帮助下,我设法创建了下一个查询:

select 'UPDATE itpserver.managed_incidents SET trigger_time = ''' || trigger_time || ''' where id = ' || id::text || ';'

from (

      select id, trigger_time + INTERVAL '120 days' trigger_time

      FROM itpserver.managed_incidents 

) a

where 1=1

group by id,trigger_time

order by id

this query created all the update queries I needed in order to update all the rows in my table.此查询创建了我需要的所有更新查询,以便更新我表中的所有行。 (the trigger_time is the date column). (trigger_time 是日期列)。

after it created all the update queries I needed, I ran all of them at the same time as a sql script在它创建了我需要的所有更新查询之后,我同时运行了所有这些查询作为 sql 脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM