PostgreSQL DATEADD() Equivalent

Updated 20 April 2024 to include the date_add() function.

SQL Server has its DATEADD() function that adds an interval to a date value. MySQL’s DATE_ADD() and ADDDATE() for does the same thing, as does MariaDB’s DATE_ADD() and ADDDATE(). SQLite has a DATE() function that also provides the option of adding an interval to a given date.

Prior to version 16, PostgreSQL didn’t have a DATEADD() or equivalent function. But with PostgreSQL 16 came with the introduction of the date_add() function, which allows us to add an interval to a timestamp with time zone.

We can also add and subtract values from dates with date/time operators such as + and -.

Continue reading

Fix “ERROR:  each UNION query must have the same number of columns” in PostgreSQL

When using the UNION operator in PostgreSQL, if you encounter an error that reads “ERROR:  each UNION query must have the same number of columns“, it’s because there’s a mismatch in the number of columns returned by the queries on either side of the UNION operator.

This error occurs when the number of columns returned by each SELECT statement is different.

The way to fix this is to ensure that both SELECT statements return the same number of columns.

Continue reading