Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pcUKx-0006fI-7X for pgsql-general@arkaria.postgresql.org; Wed, 15 Mar 2023 16:50:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pcUKw-0007Aq-5X for pgsql-general@arkaria.postgresql.org; Wed, 15 Mar 2023 16:50:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pcUKv-0007Ah-RZ for pgsql-general@lists.postgresql.org; Wed, 15 Mar 2023 16:50:49 +0000 Received: from mout-u-204.mailbox.org ([2001:67c:2050:101:465::204]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pcUKt-0007fS-DV for pgsql-general@lists.postgresql.org; Wed, 15 Mar 2023 16:50:48 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-204.mailbox.org (Postfix) with ESMTPS id 4PcGc12bmNz9sTC; Wed, 15 Mar 2023 17:50:41 +0100 (CET) Date: Wed, 15 Mar 2023 17:50:40 +0100 (CET) From: Erik Wienhold To: magog002@web.de, pgsql-general@lists.postgresql.org Message-ID: <1053334888.488227.1678899040820@office.mailbox.org> In-Reply-To: References: Subject: Re: Removing trailing zeros (decimal places) from a numeric (pre trim_scale()) with unexpected behaviour MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Priority: 3 Importance: Normal X-Rspamd-Queue-Id: 4PcGc12bmNz9sTC List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 15/03/2023 14:51 CET magog002@web.de wrote: > > I want to remove not needed decimal places / trailing zeros from a numeric. > I know this can be done starting PG >=13 with TRIM_SCALE(numeric) which would > solve my issue (with an additional CAST to TEXT at the end). Unfortunately > the production database is still running with PostgreSQL 12.x and this is > something I currently can't change. > > So to get rid of the not needed decimal places I tried TO_CHAR(..., 'FM....') > in combination with TRUNC() as shown below with examples. This does not remove > the decimal places separator if the complete scale digits are zero (60.000). Cast the to_char result to numeric and then to text. This will also remove trailing zeros. select to_char('60.000'::numeric, 'FM999.999')::numeric::text, to_char('60.100'::numeric, 'FM999.999')::numeric::text; to_char | to_char ---------+--------- 60 | 60.1 (1 row) > The current behaviour might be intentional but it 'smells like a bug' to me. It follows Oracle's to_char behavior: select to_char('60.000', 'FM999.999') from dual; TO_CHAR('60.000','FM999.999') ----------------------------- 60. -- Erik