From: | "Francis Solomon" <francis(at)stellison(dot)co(dot)uk> |
---|---|
To: | "He weiping" <laser(at)zhengmai(dot)com(dot)cn>, <pgsql-general(at)postgresql(dot)org> |
Subject: | RE: is it a bug? |
Date: | 2000-11-29 12:32:09 |
Message-ID: | NEBBIFFPELJMCJAODNPKKEFECDAA.francis@stellison.co.uk |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general pgsql-hackers |
Hi,
I think you've misunderstood the purpose of the functions.
They exist to *pad* the strings, not to truncate them.
Your examples will both return 'laser' because char_length('laser') = 5
and you asked for a padded length of 4.
Had you done this: select lpad('laser', 8, '*');
You would get this: ***laser
... and obviously with rpad() you would have seen 'laser***' instead.
If you want to truncate strings, try this:
select substring('laser' from 1 for 4);
... which will truncate to length 4, i.e. 'lase'
I couldn't find a combination function that will perform both of these
functions in one. However, you could try a construct like this:
select rpad(substring('laser' from 1 for xx), xx, '*');
... where 'xx' is the number of characters you want in the final string.
I'm sure you could wrap a user-defined function around this to that
you'd only have to feed in the number of characters once instead of
twice. Perhaps someone else knows a better way of doing this?
Hope this helps
Francis Solomon
>I'm using cvs-current, and testing those build-in function
>according to the docs.
>but it seems the "lpad", "rpad" don't work,
>when I type:
>select lpad('laser', 4, 'a');
>in psql, the result is still
>'laser', the same with 'rpad',
>Is it a bug or I'm mis-understaning the lpad and/or rpad functions?
>
>Regards
>
>Laser
From | Date | Subject | |
---|---|---|---|
Next Message | Anand Raman | 2000-11-29 12:44:16 | insertion times .. |
Previous Message | Jose Manuel Lorenzo Lopez | 2000-11-29 12:21:52 | Re: win32 version of PG7.0.x? |
From | Date | Subject | |
---|---|---|---|
Next Message | Matthew Kirkwood | 2000-11-29 13:09:05 | Re: 8192 BLCKSZ ? |
Previous Message | xuyifeng | 2000-11-29 12:27:37 | Re: beta testing version |