1

I have this basic PostgreSQL query:

SELECT AGE('2021-01-21', '1942-11-20');

Which returns an interval in days:

output: 28531 days, 0:00:00

I am using PostgreSQL version 14, according to the docs, AGE() should return a symbolic result in years, months and days instead of just days.

Does anyone know why this interval is returned in days instead of in years, months, days?

6
  • It's duplicated for sure Commented Dec 15, 2022 at 10:15
  • Hi @oguzhan00, your comment does not really help me as you don't link me the question from which you think this is a duplicate... Commented Dec 15, 2022 at 10:59
  • Best guess is that there is another version of age() in your database. In psql do \df age and add the results as update to your question. Commented Dec 15, 2022 at 16:09
  • Another thought, in what client are you running the query? Also SELECT '2021-01-21'::date - '1942-11-20'::date; 28552 so the result for your output is incorrect. Commented Dec 15, 2022 at 17:04
  • @AdrianKlaver thanks for your comments! I'm using pyodbc v4.0.32 driver to connect to the database and run queries using Python. Commented Dec 16, 2022 at 15:24

2 Answers 2

1

Function age returns type interval which you can format as you need using to_char. Here is an illustration. I would strongly suggest that you control the presentation explicitly rather than rely on defaults.

SELECT to_char(age('2021-01-21','1942-11-20'), 'yy "years", mm "months and" dd "days"');
SELECT to_char(age('2021-01-21','1942-11-20'), 'yy-mm-dd');

Results:

78 years, 02 months and 01 days
78-02-01
Sign up to request clarification or add additional context in comments.

6 Comments

age out of the box works correctly: SELECT AGE('2021-01-21', '1942-11-20'); 78 years 2 mons 1 day, so the above is unnecessary. The issue is something else, most likely a non-standard(user created) version of age() floating around.
@AdrianKlaver Yet content and presentation are different concerns that is good to keep separate.
But your answer does not address the OP's question which is age() is not returning the value it should. You are answering what can I do with the return value of age to make it be a different format.
@AdrianKlaver It does as I am only saying that implicit "symbolic" format shall not be relied upon. Run SET intervalstyle = 'sql_standard' before SELECT AGE and see the result.
I am familiar with the intervalstyle settings. That still does not relate to the OP's question. With none of the settings will you get 28531 days, 0:00:00 using age(). There is an issue with the age function itself, most likely because there is a non-standard version of it in the database. That is the issue needing an answer.
|
0

What you're saying is correct it should display the years, months and days.

Maybe you could use this statement where you specify the format you'd like to recieve to get the desired ouput:

SELECT AGE(timestamp '2021-01-21', timestamp '1942-11-20', 'YYYY-MM-DD');

2 Comments

Thanks for your comment, unfortunately AGE() does not accept a third argument..
And what if you just use: SELECT AGE(timestamp '2021-01-21', timestamp '1942-11-20');?

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.