3

I've recently come across this way of using the interval type in PostgreSQL:

SELECT CURRENT_DATE + INTERVAL '1' month;
      ?column?       
---------------------
 2018-11-22 00:00:00

From the docs, it looks like the “proper” way to do it would be:

SELECT CURRENT_DATE + INTERVAL '1 month';
      ?column?       
---------------------
 2018-11-22 00:00:00

Are the two queries equivalent? Where's the former syntax documented?

1
  • The first one is standard SQL. The second one is PostgreSQL extension. Commented Oct 23, 2018 at 6:57

1 Answer 1

2

The first syntax is SQL standard syntax:

<interval literal> ::= INTERVAL [ <sign> ] <interval string> <interval qualifier>

<interval qualifier> ::=
    <start field> TO <end field>
  | <single datetime field>

I'll spare you the definition of <start field>, <end field> and <single datetime field>, but essentially they can take the values YEAR, MONTH, DAY, MINUTE or SECOND.

PostgreSQL doesn't support the <sign>, you'd have to add that to the string.

Here a few samples:

INTERVAL '1-10' YEAR TO SECOND
INTERVAL '20:03:15' DAY TO SECOND
INTERVAL '1' DAY
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.