Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sO3OH-00C6pT-Jz for pgsql-general@arkaria.postgresql.org; Sun, 30 Jun 2024 22:51:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sO3OF-00DhDW-Dp for pgsql-general@arkaria.postgresql.org; Sun, 30 Jun 2024 22:51:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sO3OF-00DhDO-30 for pgsql-general@lists.postgresql.org; Sun, 30 Jun 2024 22:51:23 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sO3O8-004NG0-GO for pgsql-general@lists.postgresql.org; Sun, 30 Jun 2024 22:51:22 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 45UMpE952537943; Sun, 30 Jun 2024 18:51:14 -0400 From: Tom Lane To: Igal Sapir cc: pgsql-general Subject: Re: Passing a dynamic interval to generate_series() In-reply-to: References: Comments: In-reply-to Igal Sapir message dated "Sun, 30 Jun 2024 15:39:26 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2537941.1719787874.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 30 Jun 2024 18:51:14 -0400 Message-ID: <2537942.1719787874@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Igal Sapir writes: > But this throws an error (SQL Error [42601]: ERROR: syntax error at or n= ear > "'1 '"): > SELECT generate_series( > date_trunc('month', current_date), > date_trunc('month', current_date + interval '7 month'), > interval ('1 ' || 'month')::interval > ) You're overthinking it. SELECT generate_series( date_trunc('month', current_date), date_trunc('month', current_date + interval '7 month'), ('1 ' || 'month')::interval ); generate_series = ------------------------ 2024-06-01 00:00:00-04 2024-07-01 00:00:00-04 2024-08-01 00:00:00-04 2024-09-01 00:00:00-04 2024-10-01 00:00:00-04 2024-11-01 00:00:00-04 2024-12-01 00:00:00-05 2025-01-01 00:00:00-05 (8 rows) It might help to read this: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX= -CONSTANTS-GENERIC and to experiment with what you get from the constituent elements of what you tried, rather than trying to guess what they are from generate_series's behavior. For example, select (interval '1 '); interval = ---------- 00:00:01 (1 row) select (interval '1 ' || 'month'); ?column? = --------------- 00:00:01month (1 row) regards, tom lane