agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedSelecting consecutive dates as integers in yyyymmdd format
2+ messages / 2 participants
[nested] [flat]
* Selecting consecutive dates as integers in yyyymmdd format
@ 2014-02-19 22:18 AlexK <alkuzo@gmail.com>
2014-02-19 23:25 ` Re: Selecting consecutive dates as integers in yyyymmdd format bricklen <bricklen@gmail.com>
0 siblings, 1 reply; 2+ messages in thread
From: AlexK @ 2014-02-19 22:18 UTC (permalink / raw)
To: pgsql-sql
The following code gets the job done, but I am wondering if there is a
simpler/cleaner way?
WITH start_date AS(SELECT DATE '2014-02-18' AS start_date)
select CAST( to_char(start_date + INTERVAL '1d'*i,'YYYYMMDD') AS INT) AS
end_range
from generate_series(1,100) AS i CROSS JOIN start_date;
end_range
integer
20140219
20140220
20140221
20140222
20140223
20140224
20140225
20140226
20140227
20140228
20140301
20140302
(snip)
--
View this message in context: http://postgresql.1045698.n5.nabble.com/Selecting-consecutive-dates-as-integers-in-yyyymmdd-format-t...
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Selecting consecutive dates as integers in yyyymmdd format
2014-02-19 22:18 Selecting consecutive dates as integers in yyyymmdd format AlexK <alkuzo@gmail.com>
@ 2014-02-19 23:25 ` bricklen <bricklen@gmail.com>
0 siblings, 0 replies; 2+ messages in thread
From: bricklen @ 2014-02-19 23:25 UTC (permalink / raw)
To: AlexK <alkuzo@gmail.com>; +Cc: pgsql-sql
On Wed, Feb 19, 2014 at 2:18 PM, AlexK <alkuzo@gmail.com> wrote:
> The following code gets the job done, but I am wondering if there is a
> simpler/cleaner way?
>
> WITH start_date AS(SELECT DATE '2014-02-18' AS start_date)
> select CAST( to_char(start_date + INTERVAL '1d'*i,'YYYYMMDD') AS INT) AS
> end_range
> from generate_series(1,100) AS i CROSS JOIN start_date;
>
You can use the other variation of generate_series to use dates directly:
select to_char(ymd,'YYYYMMDD') ymd from
generate_series('2014-02-18'::date,'2014-03-18'::date,'1 day'::interval)
ymd;
ymd
----------
20140218
20140219
20140220
20140221
20140222
20140223
...
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2014-02-19 23:25 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-02-19 22:18 Selecting consecutive dates as integers in yyyymmdd format AlexK <alkuzo@gmail.com>
2014-02-19 23:25 ` bricklen <bricklen@gmail.com>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox