From: Andrus <kobruleht2@hot.ee>
To: pgsql-general@postgresql.org
Subject: How to create StartsWith function for char(n) type with ending space
Date: Fri, 6 Jul 2018 11:54:36 +0300
Message-ID: <D160A7990C734025BFA0685E582771F6@dell2> (raw)
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-postgres...
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-e...
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: kobruleht2@hot.ee
Subject: Re: How to create StartsWith function for char(n) type with ending space
In-Reply-To: <D160A7990C734025BFA0685E582771F6@dell2>
References: <D160A7990C734025BFA0685E582771F6@dell2>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox