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 1jyCKk-00089i-4a for pgsql-general@arkaria.postgresql.org; Wed, 22 Jul 2020 10:50:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jyCKh-0008Ob-2S for pgsql-general@arkaria.postgresql.org; Wed, 22 Jul 2020 10:50:43 +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 1jyCKg-0008OO-MI for pgsql-general@lists.postgresql.org; Wed, 22 Jul 2020 10:50:42 +0000 Received: from portal-smtp-out.hot.ee ([194.126.101.125]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jyCKd-0006xg-CO for pgsql-general@postgresql.org; Wed, 22 Jul 2020 10:50:41 +0000 Received: from localhost (localhost [127.0.0.1]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id 05F1A1B45 for ; Wed, 22 Jul 2020 13:50:36 +0300 (EEST) X-Virus-Scanned: Debian amavisd-new at vm-hot-relay10.estpak.ee Received: from portal-smtp-out.hot.ee ([127.0.0.1]) by localhost (vm-hot-relay10.estpak.ee [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id yFN94Gwi6Voi for ; Wed, 22 Jul 2020 13:50:33 +0300 (EEST) Received: from mail.hot.ee (hot-relayhost2.estpak.ee [213.180.2.80]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id 7130D1C4D for ; Wed, 22 Jul 2020 13:50:33 +0300 (EEST) Received: from dell2 (66-63-196-88.sta.estpak.ee [88.196.63.66]) (Authenticated sender: kobruleht2) by HOT-Relayhost2.estpak.ee (Postfix) with ESMTPSA id 4BBXLK2vgfz2v3 for ; Wed, 22 Jul 2020 13:50:32 +0300 (EEST) Message-ID: <7B47085A6D564BA6BCEB1509909205BF@dell2> From: "Andrus" To: "pgsql-general" Subject: How to create function returning numeric from string containing percent character Date: Wed, 22 Jul 2020 13:49:53 +0300 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="windows-1257"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal Importance: Normal X-Mailer: Microsoft Windows Live Mail 16.4.3528.331 X-MimeOLE: Produced By Microsoft MimeOLE V16.4.3528.331 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk val function should return numeric value from string up to first non-digit character, considering first decimal point also: val('1,2TEST') should return 1.2 val('1,2,3') should return 1.2 val('-1,2,3') should return -1.2 I tried CREATE OR REPLACE FUNCTION public.VAL(value text) RETURNS numeric AS $BODY$ SELECT coalesce(nullif('0'||substring(Translate($1,',','.'), '^-?[0-9]+\.?[0-9]*$'),''),'0')::numeric; $BODY$ language sql immutable; but if string contains % character, select val('1,2%') returns 0. How to force it to return 1.2 ? It should work starting from Postgres 9.0 Posted also in https://stackoverflow.com/questions/63032072/how-to-create-function-returning-value-up-to-first-non-digit-decimal-charcater#63032126 Andrus.