Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding one year to a date field in postgresql

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:

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

but postgres return:

ERROR: column "year" does not exist

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

like image 444
Emilio Galarraga Avatar asked Jan 26 '17 01:01

Emilio Galarraga


People also ask

What is the data type for year in PostgreSQL?

Introduction to the PostgreSQL DATE data type When storing a date value, PostgreSQL uses the yyyy-mm-dd format e.g., 2000-12-31. It also uses this format for inserting data into a date column.

Does Dateadd work in PostgreSQL?

Although PostgreSQL does not provide DATEADD function similar to SQL Server, Sybase or MySQL, you can use datetime arithmetic with interval literals to get the same results.

How do I create an interval in PostgreSQL?

In PostgreSQL, the make_interval() function creates an interval from years, months, weeks, days, hours, minutes and seconds fields. You provide the years, months, weeks, days, hours, minutes and/or seconds fields, and it will return an interval in the interval data type.


1 Answers

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.

like image 113
Tim Biegeleisen Avatar answered Sep 20 '22 14:09

Tim Biegeleisen