pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Ron Johnson <ronljohnsonjr@gmail.com>
To: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Return trimmed string from the query
Date: Sun, 1 Nov 2026 07:49:26 -0800
Message-ID: <36bf0a2d-368f-4c98-bf93-531cb6f868bb@aklaver.com> (raw)
In-Reply-To: <CANzqJaBAqMtb4Nz9aUhbMEqBiuPEDsFZQ-_y_AEU5UsJN4cU0Q@mail.gmail.com>
References: <CA+FnnTwZFTaBGvaQc1eqkgFJkev+BSbsn1oTaWwbKE5BLfKNRw@mail.gmail.com>
	<CANzqJaBAqMtb4Nz9aUhbMEqBiuPEDsFZQ-_y_AEU5UsJN4cU0Q@mail.gmail.com>

On 11/1/26 7:13 AM, Ron Johnson wrote:
> On Sun, Nov 1, 2026 at 1:43 AM Igor Korot <ikorot01@gmail.com 
> <mailto:ikorot01@gmail.com>> wrote:
> 
>     hi, ALL,
>     Is there an option to return a trimmed string from the query?
> 
>     If I try to retrieve the char(10) from the table and the field only have
>     "abc", how to prevent the value to be returned as "abc     "?
> 
> 
> 
> TRIM() is the portable/standards method:
> SELECT TRIM(TRAILING ' ' FROM a) FROM foo;

*TRIM removes spaces by default so the  ' ' is not necessary((length 
thrown in to prove trim):

select trim(TRAILING 'abc  '), length(trim(TRAILING'abc  '));
  rtrim | length
-------+--------
  abc   |      3


Even shorter would be::

select rtrim('abc  '), length(rtrim('abc  '));

rtrim | length
-------+--------
  abc   |      3

> 
> There are many useful string functions:
> https://www.postgresql.org/docs/17/functions-string.html <https:// 
> www.postgresql.org/docs/17/functions-string.html>
> 
> -- 
> Death to <Redacted>, and butter sauce.
> Don't boil me, I'm still alive.
> <Redacted> lobster!


-- 
Adrian Klaver
adrian.klaver@aklaver.com






view thread (10+ messages)  latest in thread

Message-ID: <36bf0a2d-368f-4c98-bf93-531cb6f868bb@aklaver.com>
Permalink:  ../36bf0a2d-368f-4c98-bf93-531cb6f868bb@aklaver.com/
Also on:    postgresql.org/message-id/36bf0a2d-368f-4c98-bf93-531cb6f868bb@aklaver.com

 · 

reply

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: adrian.klaver@aklaver.com, ronljohnsonjr@gmail.com
  Subject: Re: Return trimmed string from the query
  In-Reply-To: <36bf0a2d-368f-4c98-bf93-531cb6f868bb@aklaver.com>
  References: <CA+FnnTwZFTaBGvaQc1eqkgFJkev+BSbsn1oTaWwbKE5BLfKNRw@mail.gmail.com> <CANzqJaBAqMtb4Nz9aUhbMEqBiuPEDsFZQ-_y_AEU5UsJN4cU0Q@mail.gmail.com> <36bf0a2d-368f-4c98-bf93-531cb6f868bb@aklaver.com>

* 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