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 1rzkHg-006OcZ-NC for pgsql-general@arkaria.postgresql.org; Wed, 24 Apr 2024 21:36:08 +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 1rzkHe-000KES-Se for pgsql-general@arkaria.postgresql.org; Wed, 24 Apr 2024 21:36:06 +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 1rzkHe-000KEK-Hp for pgsql-general@lists.postgresql.org; Wed, 24 Apr 2024 21:36:06 +0000 Received: from mail-ed1-x529.google.com ([2a00:1450:4864:20::529]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rzkHa-002mDh-Su for pgsql-general@lists.postgresql.org; Wed, 24 Apr 2024 21:36:05 +0000 Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-57225322312so312623a12.1 for ; Wed, 24 Apr 2024 14:36:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713994561; x=1714599361; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=oq2vna8bVPfAo4yavhTP6XW3T8/zbT+myjPdTUxy0to=; b=fO80QkEZSrcijWu1B9cwP2xIsE3jCc/F3ROig8Jl2rz/cFDEsyOKqDTzCZblGtxBvA ue8T9mvwA6NaoniSfItiLAYn60nF9zXtc3jUxw81kAafJhT3UmQ6+EuRIzP1TgheKK3h LNKwfYobCKoE70+5+RnWqmeaCrtL1m70v2lPNnR79v8Do4ksa8Zy4ba4XtNeBaMU42X2 LnZHdy6h/o4evpvj2QxoJVbzlkf93/rUr6g9e0i8Dg3KecPJjORBQc2n9UnJwoI10sHH 8jX8KDW0JzLAWIbv70uo5KKe8u6zvVWJZSqGz+Mc673pHkyuUUhD15cN+zncCqKzNXw7 yWXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713994561; x=1714599361; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=oq2vna8bVPfAo4yavhTP6XW3T8/zbT+myjPdTUxy0to=; b=E4tQo0+AwVcFufHxgCgG+J64VQl6CkS6eaBfl9b5z2Qif+3YYnLHL4dS7H85K6EHlD uxrZZvkMoE0xVu1hUp7MYjPumUNHrmqkc+LjRQwCpSP6+vnGjtCP0dWx4Ss8nPwDz3mj EbWgQ5YrS50/hgHjxRAv444bJ7/dKGzU3Sk6Twg14ac3ln7YRzIWjpV9Dm0soYvahyAc 88z9I65fFvu5jEo+WCd+cHzC3+/Un/3VBfBYoeQkkXfAIQFtlYKv/um2VjFLG+Ht16/d E9Lr1QJ+LhKJiVDNkWdkRbQBVRsgpijjpleKnfWIN+qiFmUVDcqI5ZTbPR/ioTEF73Fs Xxyg== X-Gm-Message-State: AOJu0YylR0GS4CUjHkwjY+nXVEmPxHP/KTk31eqrzNNUQMTHkxJeVABl BUm852xNKK0LLi2yrW40t5aX/shIxLnSlmJN7FX+MPHeAfCZxuzXA3AI1HNYdVrM2nLoSWLDCuj grCOYY9No9lIHkNdzTtc0aUAEv/I= X-Google-Smtp-Source: AGHT+IFsxjwcAou91V+09Y1lhOpTDELhrnqvRYUKl06iHO+vufUWFnJCUxob3eSp8LR4c2hqk9ra8a40UAOuG5Pu5E4= X-Received: by 2002:a50:d4d2:0:b0:56d:e6f6:f73c with SMTP id e18-20020a50d4d2000000b0056de6f6f73cmr2202463edj.42.1713994561299; Wed, 24 Apr 2024 14:36:01 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Steve Baldwin Date: Thu, 25 Apr 2024 07:35:50 +1000 Message-ID: Subject: Re: is there an immutable function to switch from date to character? To: Celia McInnis Cc: "pgsql-generallists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000f931e90616de74b5" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000f931e90616de74b5 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Apr 25, 2024 at 7:31=E2=80=AFAM Celia McInnis wrote: > create temporary table junk as select now()::date as evtdate; > > alter table junk add column chardate text GENERATED ALWAYS AS > (to_char(evtdate,'YYYY-Mon-DD')) STORED; > > ERROR: generation expression is not immutable > > Maybe this is a hack but.. b2bcreditonline=3D# create temporary table junk as select now()::date as evtdate; SELECT 1 b2bcreditonline=3D# alter table junk add column chardate text GENERATED ALWAYS AS (to_char(evtdate,'YYYY-Mon-DD')) STORED; ERROR: generation expression is not immutable b2bcreditonline=3D# create or replace function date_to_text(i_date in date) returns text immutable language sql as $$ select to_char(i_date, 'YYYY-MM-DD') $$; CREATE FUNCTION b2bcreditonline=3D# alter table junk add column chardate text GENERATED ALWAYS AS (date_to_text(evtdate)) STORED; ALTER TABLE b2bcreditonline=3D# select * from junk; evtdate | chardate ------------+------------ 2024-04-24 | 2024-04-24 (1 row) --000000000000f931e90616de74b5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Thu, Apr 25, 2024 at 7:31=E2=80=AF= AM Celia McInnis <celia.mcinn= is@gmail.com> wrote:
create temporary table junk as select now= ()::date as evtdate;

alter table junk add column c= hardate text GENERATED ALWAYS AS (to_char(evtdate,'YYYY-Mon-DD')) S= TORED;

ERROR: =C2=A0generation expression is not immuta= ble

Maybe this is a hack but..

b2bcre= ditonline=3D# create temporary table junk as select now()::date as evtdate;=
SELECT 1
b2bcreditonline=3D# alter table junk add column chardate te= xt GENERATED ALWAYS AS (to_char(evtdate,'YYYY-Mon-DD')) STORED;
= ERROR: =C2=A0generation expression is not immutable
b2bcreditonline=3D# = create or replace function date_to_text(i_date in date) returns text immuta= ble language sql as $$ select to_char(i_date, 'YYYY-MM-DD') $$;
= CREATE FUNCTION
b2bcreditonline=3D# alter table junk add column chardate= text GENERATED ALWAYS AS (date_to_text(evtdate)) STORED;
ALTER TABLEb2bcreditonline=3D# select * from junk;
=C2=A0 evtdate =C2=A0 | =C2=A0c= hardate
------------+------------
=C2=A02024-04-24 | 2024-04-24
(1= row)
=C2=A0
--000000000000f931e90616de74b5--