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 1j49iR-0000n7-Om for pgsql-general@arkaria.postgresql.org; Tue, 18 Feb 2020 20:43:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j49iQ-0006AZ-Hl for pgsql-general@arkaria.postgresql.org; Tue, 18 Feb 2020 20:43:34 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j49iO-00069F-FV for pgsql-general@lists.postgresql.org; Tue, 18 Feb 2020 20:43:34 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j49iG-0003qA-VU for pgsql-general@lists.postgresql.org; Tue, 18 Feb 2020 20:43:31 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 1B832608; Tue, 18 Feb 2020 15:43:23 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 18 Feb 2020 15:43:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=3 +vlYD1WDUsVVvi2A7vMoOgb8qtUHSe82xFkg4GXigw=; b=gkRXfgNixMUVpfzXi Y6JRke/rObox9JAejr0/k6LnNQTb9In0P1/ZfKTfp9HsAI8qOeb21Gm+Ea6ZggEA p3YfLG/fGh5ghEIW9wb0KDAVrcVqbcs3M+Am9iPK8gmEqIPu+qy+cw6vMsaG3AGE e+LpGt+BKIk3Ywovc7f+5P19HbmESyQi0pL3vIFLll6rw9WIx7dMIMhdg6jzUejh JzWYwmIT7Xfq/F/T5mKexD255tjtPcTEUMcRBAydCW8inv2HrEtJKqvEbd6BkRNb vi9Br1dI+euI+72t+3Jr82dnsNYz+gShV07bj2FrvDkPKdhWrLwp+03Fa2kguNyN dUnMg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=3+vlYD1WDUsVVvi2A7vMoOgb8qtUHSe82xFkg4GXi gw=; b=BTeXdk+wYU1cackVKvo3RyV/CknvGZ5PZnmBU8XAu75H8VuHTV81T347+ WlkNdUYOhsHJoxQFJVUMuaUh0lTZiHHJuoi49Mcej9VXdspVOswQUpH15L/G4dYN dKhOV0mo9IiLTZA95qQ/9RlZ5CVyEg4NK/lOK/39OGZt4zYXahv3bzYuZxQGtYhc 2ERrIxzzBxhTX0KkUlgqMUV/8Dp6/ralGrKYvYgthxo2qnKo6aF9fpsalw+TuY1x RxQn+nroEQJlkOLGm8UVo4CJpZFpB9ibVljwTSA1HtsbmOvRMo6G9fPsx8/agb3i ko7j74kftvy38FRgM3y3MwOpSoUxg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrjeekgddugedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefuvfhfhffkffgfgggjtgfgsehtke ertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuffhomhgrihhnpehpohhsthhgrhgvsh hqlhdrohhrghenucfkphepjedurddvuddvrdduvdelrdduleefnecuvehluhhsthgvrhfu ihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrh esrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.10] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id 18C9A3060C21; Tue, 18 Feb 2020 15:43:22 -0500 (EST) Subject: Re: How to handle CASE statement with PostgreSQL without need for typecasting To: John W Higgins , pgsql-general References: From: Adrian Klaver Message-ID: <38f8b6a5-a9b5-c8dd-de22-84807e70cfd4@aklaver.com> Date: Tue, 18 Feb 2020 12:43:21 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2/18/20 10:51 AM, John W Higgins wrote: > 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 Yes and no: test=> UPDATE t_update SET F1 = '02/23/2020'; UPDATE 1 UPDATE 1 test=> select pg_typeof('02/23/2020'); pg_typeof ----------- unknown test=> UPDATE t_update SET F1 = '02/23/2020'::unknown; UPDATE 1 test=> UPDATE t_update SET F1 = (CASE WHEN ('02/18/2020' IS NULL ) THEN ('02/18/2020' ) ELSE ('02/19/2020') END)::unknown; ERROR: failed to find conversion function from unknown to timestamp without time zone test=> UPDATE t_update SET F1 = (CASE WHEN ('02/18/2020' IS NULL ) THEN ('02/18/2020' ) ELSE ('02/19/2020') END)::timestamp; UPDATE 1 So there is some sort of different evaluation going on in the CASE statement. > 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 > -- Adrian Klaver adrian.klaver@aklaver.com