Re: Fault with initcap

From: Metin Ulusinan <metin(dot)ulusinan(at)ssicilian(dot)net>
To: Shaozhong SHI <shishaozhong(at)gmail(dot)com>
Cc: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: Re: Fault with initcap
Date: 2021-10-13 09:38:59
Message-ID: CAPi93JRY5yQG6iA+bmx6KkVS-TK3zF6EHxR-oYuBhamoCa+jhA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

Hi,
Yes, this can be adaptable, and i did simple version of this.
It just split text words with find spaces, capitalise each
pieces(word) and merge together again.
This is a quick and simple work. You can develop over it about your needs.

Try that and tell us about result.

CREATE OR REPLACE FUNCTION initcap2(text)
RETURNS text
LANGUAGE plpgsql
AS $function$
DECLARE
sentence TEXT := '';
word_array TEXT[];
word TEXT;
word_out TEXT;
BEGIN
sentence := $1;

IF sentence is NULL THEN
RETURN NULL;
END IF;

word_array := regexp_split_to_array($1, E'\\s+');
FOREACH word IN ARRAY word_array
LOOP
word_out := upper(left(word, 1)) || lower(substring(word, 2));
sentence := regexp_replace(sentence, word, word_out);
END LOOP;

RETURN trim(sentence);
END;
$function$
;

On Wed, Oct 13, 2021 at 11:48 AM Shaozhong SHI <shishaozhong(at)gmail(dot)com>
wrote:

>
>
> On Wed, 13 Oct 2021 at 07:33, Metin Ulusinan <metin(dot)ulusinan(at)ssicilian(dot)net>
> wrote:
>
>> I tried both on PostgreSQL 11.13 and yes "Sainsbury's bank" 's "S" is
>> buggy, but notemachine is not.
>>
>> Will anyone try @ 14?
>>
>>
>> n s version
>> Notemachine Sainsbury'S Bank PostgreSQL 11.13 (Debian 11.13-0+deb10u1)
>> on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
>>
>>
>> Hello, Metin Ulusinan,
>>
>
> I can confirm that you pin-out the issue.
>
> There is a INITCAP2. Alternative function to PostgreSQL builtin
> initcap(text) with support for accented words. · GitHub
> <https://gist.github.com/pv8/8291531>
>
> Can this be adapted to capitalise the first letter of each work in a
> string?
>
> Regards,
>
> David
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message hubert depesz lubaczewski 2021-10-13 09:52:15 Re: Grant select for all tables of the 12 schemas of my one db ?
Previous Message celati Laurent 2021-10-13 09:04:42 Grant select for all tables of the 12 schemas of my one db ?

Browse pgsql-sql by date

  From Date Subject
Next Message Jain, Ankit 2021-10-13 16:17:50 RE: Fault with initcap
Previous Message Shaozhong SHI 2021-10-13 08:48:25 Re: Fault with initcap