public inbox for [email protected]  
help / color / mirror / Atom feed
Passing a dynamic interval to generate_series()
2+ messages / 2 participants
[nested] [flat]

* Passing a dynamic interval to generate_series()
@ 2024-06-30 22:39 Igal Sapir <[email protected]>
  2024-07-01 07:37 ` Re: Passing a dynamic interval to generate_series() Shammat <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: Igal Sapir @ 2024-06-30 22:39 UTC (permalink / raw)
  To: pgsql-general <[email protected]>

Hello,

I am trying to pass a dynamic interval to generate_series() with date range.

This works as expected, and generates a series with an interval of 1 month:

SELECT generate_series(
    date_trunc('month', current_date),
    date_trunc('month', current_date + interval '7 month'),
    interval '1 month'
)


This works as expected and returns an interval of 1 month:

SELECT ('1 ' || 'month')::interval;


But this throws an error (SQL Error [42601]: ERROR: syntax error at or near
"'1 '"):

SELECT generate_series(
    date_trunc('month', current_date),
    date_trunc('month', current_date + interval '7 month'),
    interval ('1 ' || 'month')::interval
)


And this returns a series with interval of 1 second??

SELECT generate_series(
    date_trunc('month', current_date),
    date_trunc('month', current_date + interval '7 month'),
    (interval '1 ' || 'month')::interval
)

Because this returns an interval of 1 second:

SELECT (interval '1 ' || 'month')::interval;

Is that a bug?


I am able to work around the issue using a CASE statement, but shouldn't it
work simply by concatenating the string with the || operator?

Thank you,

Igal


^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Passing a dynamic interval to generate_series()
  2024-06-30 22:39 Passing a dynamic interval to generate_series() Igal Sapir <[email protected]>
@ 2024-07-01 07:37 ` Shammat <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Shammat @ 2024-07-01 07:37 UTC (permalink / raw)
  To: [email protected]


Igal Sapir schrieb am 01.07.2024 um 00:39:
> I am trying to pass a dynamic interval to generate_series() with date range.
>
> This works as expected, and generates a series with an interval of 1 month:
>
> SELECT generate_series(
>     date_trunc('month', current_date),
>     date_trunc('month', current_date + interval '7 month'),
>     interval '1 month'
> )
>
>
> This works as expected and returns an interval of 1 month:
>
> SELECT ('1 ' || 'month')::interval;
>
>
> But this throws an error (SQL Error [42601]: ERROR: syntax error at or near "'1 '"):
>
> SELECT generate_series(
>     date_trunc('month', current_date),
>     date_trunc('month', current_date + interval '7 month'),
>     interval ('1 ' || 'month')::interval
> )

I am a fan of make_interval() when it comes to creating intervals from dynamic parameters:

SELECT generate_series(
    date_trunc('month', current_date),
    date_trunc('month', current_date + interval '7 month'),
    make_interval(months => 1)
)


The value for make_interval() can e.g. passed as a parameter from your programming language.








^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2024-07-01 07:37 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-06-30 22:39 Passing a dynamic interval to generate_series() Igal Sapir <[email protected]>
2024-07-01 07:37 ` Shammat <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox