Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL 9.3: Add/Remove QUARTER to/from timestamp

I want to add the QUARTER to the TIMESTAMP in PostgreSQL 9.3 version.

My Try:

For adding:

SELECT TIMESTAMP '2016-01-01' + INTERVAL '01 QUARTER';

For remove:

SELECT TIMESTAMP '2016-01-01' - INTERVAL '01 QUARTER';
********** Error **********  
ERROR: invalid input syntax for type interval: "01 QUARTER"  
SQL state: 22007  
Character: 42
like image 402
MAK Avatar asked Jan 01 '16 06:01

MAK


People also ask

How do I split a date and time in PostgreSQL?

This can be done in PostgreSQL using the AGE() function. This function takes in two timestamps as arguments and then returns the interval between them.

What is interval in PostgreSQL?

In PostgreSQL the interval data type is used to store and manipulate a time period. It holds 16 bytes of space and ranging from -178, 000, 000 years to 178, 000, 000 years. It also has additional attribute called “precision (denoted by p)” that can be used to set the level of precision in the query results.

What is Current_timestamp in PostgreSQL?

The PostgreSQL CURRENT_TIMESTAMP() function returns the current date and time with time zone. It is important to note that the time and time zone returned by this function is from the time the transactions start.

How do I change the date format in PostgreSQL?

You can change the format in the postgresql. conf file. The date/time styles can be selected by the user using the SET datestyle command, the DateStyle parameter in the postgresql. conf configuration file, or the PGDATESTYLE environment variable on the server or client.


1 Answers

That's because quarter is not among supported units for interval input. The manual:

unit is microsecond, millisecond, second, minute, hour, day, week, month, year, decade, century, millennium, or abbreviations or plurals of these units;

Use '3 month' (or similar) instead:

SELECT timestamp '2016-01-01' + interval '3 month';
like image 93
Erwin Brandstetter Avatar answered Oct 27 '22 20:10

Erwin Brandstetter