Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j47yW-0005NP-Kh for pgsql-general@arkaria.postgresql.org; Tue, 18 Feb 2020 18:52:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j47yV-0007bn-Eb for pgsql-general@arkaria.postgresql.org; Tue, 18 Feb 2020 18:52:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j47yV-0007bg-50 for pgsql-general@lists.postgresql.org; Tue, 18 Feb 2020 18:52:03 +0000 Received: from mail-oi1-x230.google.com ([2607:f8b0:4864:20::230]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1j47yS-00053U-2J for pgsql-general@lists.postgresql.org; Tue, 18 Feb 2020 18:52:02 +0000 Received: by mail-oi1-x230.google.com with SMTP id b18so21191976oie.2 for ; Tue, 18 Feb 2020 10:51:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=CfxhxhJiSqQIk1wmlRfFjVktklNxmYnuQxDDtoiZFzs=; b=pVm7JW0KNKGa7thJ8rYXhRmGOpcDh4N4CbWiRPGiOxDpL+Pk2hC18mAsHcCUiyQbPk MeXdCwMEbaGfCgFun1rh1incjjy2UZrfP9ywBYokM+5yLxv9TLHoyASRPdl215ZjU4Gc 9XgZPRrlLL7eDgbxCcJeXYIhSXqcYeJhkS3LC2KJ5CXOw6nl9Y34pUrT/MoCx3s6D7TX g/xZwB9o+m0VE/i8mheZwA2AINu0FdvhXjGBtY2EMGuhkEhU5HaXPbHkg7KkjSRBz8dz sqqvBJ7b6InKijGRan3ze/rO89evud1uI9sOf0Jyr+K1cdOuoDuWIbqKLbJhM4nz/Jg9 g1qA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=CfxhxhJiSqQIk1wmlRfFjVktklNxmYnuQxDDtoiZFzs=; b=RbE20Z9hxYDxKeWBmTk3l4AvIDY5wL+fwIKuVSBcTOTphPljbvII+rmnbqCKSsE/rO g0nD95OuoL9/dvMFxnOqCLi3zTfpqiKmVqvWzYoXLDJbzLQbmiqYkOlFLvT/AAi1g2Rp XZRF58u4ZvWadBPp9ewTNSsqTFEHOFKzEOUVLxx4QOyZgIQyhgyBPTVhyvQH+eeB7ezG 7eWgxRyc1OC1Hf+YhnDbBseV/FhpL9zu8e0SDR3fZV5Jx1E4vtDtPMmC848rhSAPf+SV 5z6Wg/deIRXi4XC/FQOh9fX2GT6BBYWfAjGpey4zkrvzKckVXfnawouOWEHPiWfEJfjh /mSg== X-Gm-Message-State: APjAAAWZeeuTw/29dyilAxSDx0lPmqAFWGhYP9VdYwczbGsdaYXK+C20 m1XoYinc9ffLzYa7GnXkJBeLDx/zmsTT4/kRkHa4fLYR X-Google-Smtp-Source: APXvYqzXCNUh6GiTm2xk57Ys9E52cB8HaVMJ3gi65FDHQiHVdJO+456nTIVfkVRxyuPnGZSaPDpua9qq09+AIZaQ0qI= X-Received: by 2002:aca:4f4c:: with SMTP id d73mr2221544oib.107.1582051917823; Tue, 18 Feb 2020 10:51:57 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: John W Higgins Date: Tue, 18 Feb 2020 10:51:46 -0800 Message-ID: Subject: Re: How to handle CASE statement with PostgreSQL without need for typecasting To: pgsql-general Content-Type: multipart/alternative; boundary="0000000000009401fe059ede2a5c" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --0000000000009401fe059ede2a5c Content-Type: text/plain; charset="UTF-8" Good Morning, > > NOTE: From my research online, I found that typecasting works and also > the error from the database suggests typecasting. > > This statement works: > > UPDATE t_update SET F1 = (CASE WHEN (?::timestamp(6) IS NULL ) THEN > (?::timestamp(6) ) ELSE (?::timestamp(6) ) END) > > There is no option to convert the text parameter to a timestamp - you need to cast it - or use a parsing function or something else - but a text value cannot drop directly into a timestamp column. But it's not the case statement that is the issue - but rather the update - so you could shorten the statement a little with this. UPDATE t_update SET F1 = (CASE WHEN (? IS NULL ) THEN (?) ) ELSE (?) ) END) ::timestamp(6) You don't need a timestamp until you place in in the column. You also probably don't want a case statement here - not the standard option for this UPDATE t_update SET F1 = COALESCE(?, ?)::timestamp(6) with the first ? being the when above and the second being the else above. See here [1] John [1] - https://www.postgresql.org/docs/current/functions-conditional.html > --0000000000009401fe059ede2a5c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Good Morning,
=C2=A0

NOTE:=C2= =A0 From my research online, I found that typecasting works and also the er= ror from the database suggests typecasting.

This statement works:

UPDATE t_update SET F1 =3D (CASE WHEN (?:= :timestamp(6) IS NULL ) THEN (?::timestamp(6) ) ELSE (?::timestamp(6) ) END= )=C2=A0=C2=A0=C2=A0

=
There is no option to convert the text parameter to a timest= amp - you need to cast it - or use a parsing function or something else - b= ut a text value cannot drop directly into a timestamp column. But it's = not the case statement that is the issue - but rather the update - so you could shorten the statement a little with= this.

UPDATE t_update SET F1 =3D (CASE WHEN= (? IS NULL ) THEN (?) ) ELSE (?) ) END)::timestamp(6)

You don&#= 39;t need a timestamp until you place in in the column.

You also probably don't want a case statement here - not= the standard option for this=C2=A0

UPDAT= E t_update SET F1 =3D COALESCE(?, ?)::timestamp(6) with the first ? being t= he when above and the second being the else above. See here [1]

John

--0000000000009401fe059ede2a5c--