Re: bug in substring???

Lists: pgsql-hackers
From: "scott(dot)marlowe" <scott(dot)marlowe(at)ihs(dot)com>
To: <pgsql-hackers(at)postgresql(dot)org>
Subject: bug in substring???
Date: 2004-02-06 21:21:43
Message-ID: Pine.LNX.4.33.0402061419460.6661-100000@css120.ihs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: Postg범퍼카 토토SQL

I'm using substring. Since I'm a coder more than a database guy, I
expected this:

select substring('abcdefgh',0,4);

would give me

abcd

but it gives me a left aligned 'abc'

select substring('abcdefgh',1,4);

works fine.

select substring('abcdefgh',-4,4);

gives me nothing. Shouldn't a negative offset, or even 0 offset result in
an error or something here? Or is there a special meaning to a negative
offset I'm not getting?

Just wondering.


From: Joe Conway <mail(at)joeconway(dot)com>
To: "scott(dot)marlowe" <scott(dot)marlowe(at)ihs(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: bug in substring???
Date: 2004-02-06 23:09:57
Message-ID: 40241EC5.7000003@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

scott.marlowe wrote:
> gives me nothing. Shouldn't a negative offset, or even 0 offset result in
> an error or something here? Or is there a special meaning to a negative
> offset I'm not getting?

In varlena.c there is this comment:

* text_substr()
* Return a substring starting at the specified position.
* - thomas 1997-12-31
*
* Input:
* - string
* - starting position (is one-based)
* - string length
*
* If the starting position is zero or less, then return from the start
* of the string adjusting the length to be consistent with the
* "negative start" per SQL92. If the length is less than zero, return
* the remaining string.

Joe


From: "scott(dot)marlowe" <scott(dot)marlowe(at)ihs(dot)com>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: bug in substring???
Date: 2004-02-06 23:48:13
Message-ID: Pine.LNX.4.33.0402061647220.7563-100000@css120.ihs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, 6 Feb 2004, Joe Conway wrote:

> scott.marlowe wrote:
> > gives me nothing. Shouldn't a negative offset, or even 0 offset result in
> > an error or something here? Or is there a special meaning to a negative
> > offset I'm not getting?
>
> In varlena.c there is this comment:
>
> * text_substr()
> * Return a substring starting at the specified position.
> * - thomas 1997-12-31
> *
> * Input:
> * - string
> * - starting position (is one-based)
> * - string length
> *
> * If the starting position is zero or less, then return from the start
> * of the string adjusting the length to be consistent with the
> * "negative start" per SQL92. If the length is less than zero, return
> * the remaining string.

thanks. I just got done looking up the SQL explanation, and I think my
head exploded. Thanks for the heads up.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "scott(dot)marlowe" <scott(dot)marlowe(at)ihs(dot)com>
Cc: Joe Conway <mail(at)joeconway(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: bug in substring???
Date: 2004-02-07 06:39:13
Message-ID: 17305.1076135953@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"scott.marlowe" <scott(dot)marlowe(at)ihs(dot)com> writes:
> thanks. I just got done looking up the SQL explanation, and I think my
> head exploded. Thanks for the heads up.

The formal definition seems unnecessarily complicated :-(, but the spec
authors' intent is reasonably clear from this paragraph in the
"Concepts" section of SQL92:

<character substring function> is a triadic function, SUBSTRING,
that returns a string extracted from a given string according
to a given numeric starting position and a given numeric length.
Truncation occurs when the implied starting and ending positions
are not both within the given string.

In other words, they consider that a zero or negative start position
should be truncated back to the actual start position (1) in much the
same way that a too-large length specification would be truncated to
match the actual end position.

AFAICT the only case in which SUBSTRING is supposed to raise an error is
when you specify a negative length.

regards, tom lane