Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fbOBQ-0003Xv-CQ for pgsql-general@arkaria.postgresql.org; Fri, 06 Jul 2018 10:41:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fbOBM-0001iM-Ps for pgsql-general@arkaria.postgresql.org; Fri, 06 Jul 2018 10:41:44 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fbMW1-00074a-Va for pgsql-general@lists.postgresql.org; Fri, 06 Jul 2018 08:54:58 +0000 Received: from portal-smtp-out.hot.ee ([194.126.101.122]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fbMVt-0006Bs-Le for pgsql-general@postgresql.org; Fri, 06 Jul 2018 08:54:56 +0000 Received: from localhost (localhost [127.0.0.1]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id 69FE0E9C for ; Fri, 6 Jul 2018 11:54:45 +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 G4+rVzJqtSdF for ; Fri, 6 Jul 2018 11:54:43 +0300 (EEST) Received: from mail.hot.ee (hot-relayhost1.estpak.ee [213.180.2.79]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id 4FDE6148E for ; Fri, 6 Jul 2018 11:54:43 +0300 (EEST) Received: from dell2 (66.63.196.88.sta.estpak.ee [88.196.63.66]) (Authenticated sender: kobruleht2) by HOT-Relayhost1.estpak.ee (Postfix) with ESMTPSA id 450A86A3 for ; Fri, 6 Jul 2018 11:54:41 +0300 (EEST) Message-ID: From: "Andrus" To: Subject: How to create StartsWith function for char(n) type with ending space Date: Fri, 6 Jul 2018 11:54:36 +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 Hi! I need to create startswith function which returns true if char(n) database column starts with some characters which may can contain space at end. Spaces should treated like other characters. Using sample data below startswith( test, 'A') startswith( test, 'A ') StartsWith(test, rpad('A',19) ) shuld return true but startswith( test, RPAD( 'A', 20)) should return false since there is extra space in end of check string Database contains test column which has char(20) type column and this cannot changed. I tried code below but it returns false. How to fix this so that it returns true? Using Postgres starting from 9.1 Andrus. CREATE or replace FUNCTION public.likeescape( str text ) -- https://stackoverflow.com/questions/10153440/how-to-escape-string-while-matching-pattern-in-postgresql RETURNS text AS $$ SELECT replace(replace(replace($1,'^','^^'),'%','^%'),'_','^_') ; $$ LANGUAGE sql IMMUTABLE; CREATE or replace FUNCTION public.StartWith( cstr text, algusosa text ) RETURNS bool AS $$ SELECT $2 is null or $1 like likeescape($2) ||'%' ESCAPE '^' ; $$ LANGUAGE sql IMMUTABLE; create temp table test ( test char(20) ) on commit drop; insert into test values ('A' ); select StartWith(test, 'A ' ) from test posted also in https://stackoverflow.com/questions/51206529/how-to-create-startswith-function-for-charn-type-with-ending-space