user2641077
user2641077

Reputation: 159

Comparing dates in postgresql

I have the following query:

SELECT array_to_string(array( SELECT birthday from users where userid>10), ',' ) as birthdaylist

This generate for example the following data:

birthdaylist
text
"2015-08-02,2015-08-09"

What I want is to get this result:

("02/Aug/2015","09/Aug/2015")

I read the postgresql manual http://www.postgresql.org/docs/8.2/static/functions-formatting.html I see there is a function to_date('05 Dec 2000', 'DD Mon YYYY') but it doesn't really help my case.

I tried:

Select Array (SELECT birthday from users where userid>10)

This query gave me an array but it's not a text array its: date[] How do i get ("02/Aug/2015","09/Aug/2015") ?

Upvotes: 0

Views: 87

Answers (1)

user330315
user330315

Reputation:

You are overcomplicating your query. You don't need a nested sub-select. Just do a regular aggregation with the format you want.

select string_agg(to_char(birthday, 'DD Mon YYYY'), ',')
from users 
where userid > 10;

Btw: you should not read the manual for a completely outdated version (8.2). Always use the manual for the version you are using.

Upvotes: 3

Related Questions