Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL Subtract exactly a year from a date field PSQL

I tried to do SQL Subtract exactly a year but the syntax in PSQL didn't work. In my case dates were imported with 2 digit years so they all have birthdays off by 1900 years.

UPDATE patients SET birthday = dateadd(year, 100, getdate(birthday)

throws:

ERROR:  syntax error at or near "table"
LINE 1: UPDATE table SET datefield = DATE_ADD(datefield, INTERVAL 1 ...
like image 751
lacostenycoder Avatar asked Aug 14 '17 23:08

lacostenycoder


1 Answers

This works in PostgreSQL by simply using INTERVAL and passing in a string of increment values and type (seconds, minutes, hours, days, months, years) etc.

UPDATE patients SET birthday = birthday + INTERVAL '1900 years';

Found idea here https://www.postgresql.org/docs/9.1/static/functions-datetime.html

like image 77
lacostenycoder Avatar answered Sep 22 '22 01:09

lacostenycoder