将一年添加到 postgresql 中的日期字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41864550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:22:05  来源:igfitidea点击:

Adding one year to a date field in postgresql

postgresqldatedateadd

提问by Emilio Galarraga

I have a table in postgresql with a field_date using the syntax 'YYYY-MM-DD', I want to add a year to the field with the the sentence:

我在 postgresql 中有一个带有 field_date 的表,使用语法“YYYY-MM-DD”,我想用以下语句在字段中添加一年:

UPDATE table SET date_field = DATEADD(YEAR, 1, date_field);

更新表 SET date_field = DATEADD(YEAR, 1, date_field);

but postgres return:

但 postgres 返回:

ERROR: column "year" does not exist

错误:“年份”列不存在

I can't see what's wrong with the sentence

我看不出这句话有什么问题

回答by Tim Biegeleisen

Try this:

尝试这个:

UPDATE table SET date_field = date_field + interval '1 year'

It appears that you were trying to use SQL Server's DATEADD()function, which does not exist in Postgres.

看来您正在尝试使用 SQL Server 的DATEADD()功能,而 Postgres 中不存在该功能。