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 1rbm5Y-00GvZU-D2 for pgsql-general@arkaria.postgresql.org; Sun, 18 Feb 2024 18:40:33 +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 1rbm5W-00At7n-U4 for pgsql-general@arkaria.postgresql.org; Sun, 18 Feb 2024 18:40:30 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rbm5V-00At7e-71 for pgsql-general@lists.postgresql.org; Sun, 18 Feb 2024 18:40:30 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rbm5S-007P30-4I for pgsql-general@lists.postgresql.org; Sun, 18 Feb 2024 18:40:28 +0000 Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailout.nyi.internal (Postfix) with ESMTP id B47975C0069; Sun, 18 Feb 2024 13:40:24 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Sun, 18 Feb 2024 13:40:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1708281624; x=1708368024; bh=sJmT7gDcHRrmH7PB+NOC718cNhy7qXulfLyM9YAL8NU=; b= iZRn5nltlu0Lcq6QOZCf/M60wJlkJwpFBwF76tsSaB/DE+Pd063etAknBHgRW+Zx YMivWhL3dLXxk2JP3m5Ph1DqU+q7TGFPXk4BAHub6VasMWNuoN/zzfBzc6m8MzqT O1nUqgp29B3YIEYgPX2IPx0BRFIgwqT/uS4/yEBVQuGByyJ1P8ATBfKP5j12DwQb EP9T4AmXB9rGg5FU/JZU+fvNDuBMNHobL78tA056H76ACK8Ga62ZuKUlxOD3WifD +w0CW0BZRs4m4uPzitjTNCZYzxBLuLxrGEfh8IYATEdEAWqsh7IpbighQOyXGHuY kffWkksnf98Bk7ZLXtr0tw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1708281624; x= 1708368024; bh=sJmT7gDcHRrmH7PB+NOC718cNhy7qXulfLyM9YAL8NU=; b=b YND2sfnypfxpC1q1PybcRIZM89OvWLfnjz+7X5lj7m4wxMIPXo9J1KCiPD/GBgpc 6xZ94TOQnIQ9L27FOULH+mFc4J87+vsQcIE/r9KcGNl+ZvmR7OS7LdL0G1NYxZkT 0921ACEw5qq6WdYfPA8T9Qcp6HdFJaTtcoD6MMi9/+OkYDvAFXSMXCgiPqfZUJIt +Zr5ltaD29JeSfhygKFKHRZdh3umkpfxu6W1k+SSsLpgILBacKeNuEr16F1M7wZv 3yz/Ux8RysTVs9sCWkO9kboTBMqwpyEX6wKNZDcCaLtRaLqwiM0c2yK8efufOWRR 75Z+AG8Qa2+eNLUDp7x3A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdeigdduudekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefkffggfgfuvfhfhfgjtgfgsehtje ertddtvdejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhepiedvhfeihe ehgeeuieeljeeitedtjeehudegfeelkedvleekhedtgfeiffefkedunecuvehluhhsthgv rhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvh gvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 18 Feb 2024 13:40:24 -0500 (EST) Message-ID: <1a7f0c3f-f241-4e5c-a38b-7468ccedcdff@aklaver.com> Date: Sun, 18 Feb 2024 10:40:23 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Function inserting into tstzrange ? (syntax error at or near...) Content-Language: en-US To: Laura Smith , postgre References: <5Tfn0cxmVyZ3uSyl4_GqlisTneXWCuH0I5Jk6PgEGtFXkBGlTXiXfT8ffk1YyF1zhGoGOxdpmpOPvOQfK4l74T4fK8xKMxTHcs-J_08M8j4=@protonmail.ch> <3883445.1708278779@sss.pgh.pa.us> <2nqXMmBAg_UsRbjwNzuT1pU8YZwfkxPEdD-dTlTloMylGJP_99629l9bZEpUrXIoOM-SmnvWNk9fCwBMq7-5zk1au04wAh790LINW36_Zmc=@protonmail.ch> From: Adrian Klaver In-Reply-To: <2nqXMmBAg_UsRbjwNzuT1pU8YZwfkxPEdD-dTlTloMylGJP_99629l9bZEpUrXIoOM-SmnvWNk9fCwBMq7-5zk1au04wAh790LINW36_Zmc=@protonmail.ch> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2/18/24 10:30, Laura Smith wrote: > >> >> There's not bespoke SQL syntax for constructing a range. You must >> use a function, something like >> >> VALUES(p_event_id, tstzrange(p_start_time,p_end_time,'[)')) ... > > > Thanks all for your swift replies. > > Serves me right for assuming I could use variable substitution where text would normally go, i.e. I thought I could just mimic the below example from the docs by substituting the variables: > > INSERT INTO reservation VALUES > (1108, '[2010-01-01 14:30, 2010-01-01 15:30)'); Yeah, a quick and dirty example: \d event_sessions Table "public.event_sessions" Column | Type | Collation | Nullable | Default -------------------+--------------------------+-----------+----------+--------- event_id | text | | not null | evt_sess_id | text | | not null | evt_sess_times | tstzrange | | not null | evt_sess_inserted | timestamp with time zone | | not null | now() Indexes: "event_sessions_pkey" PRIMARY KEY, btree (evt_sess_id) CREATE OR REPLACE FUNCTION public.new_event_session(p_event_id text, p_start_time timestamp with time zone, p_end_time timestamp with time zone, p_sess_title text, p_sess_desc text) RETURNS text LANGUAGE plpgsql AS $function$ DECLARE v_session_id text; BEGIN EXECUTE format('INSERT INTO event_sessions(event_id, evt_sess_id, evt_sess_times) VALUES($1, 2, tstzrange($2, $3)) RETURNING evt_sess_id') INTO v_session_id USING p_event_id, p_start_time, p_end_time; RETURN v_session_id; END; $function$ select new_event_session('1', '2024-02-18', '2024-02-20', 'test', 'test desc'); new_event_session ------------------- 2 > > Lesson learnt ! > > Thanks again. > > -- Adrian Klaver adrian.klaver@aklaver.com