Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i0Qvp-0006ku-Iu for pgsql-bugs@arkaria.postgresql.org; Wed, 21 Aug 2019 13:45:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i0Qvo-0000SL-6M for pgsql-bugs@arkaria.postgresql.org; Wed, 21 Aug 2019 13:45:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i0Qvn-0000SE-Us for pgsql-bugs@lists.postgresql.org; Wed, 21 Aug 2019 13:45:43 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i0QvO-0002hS-FV for pgsql-bugs@lists.postgresql.org; Wed, 21 Aug 2019 13:45:43 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x7LDjDSw029037; Wed, 21 Aug 2019 09:45:14 -0400 From: Tom Lane To: Ashutosh Sharma cc: bvimalvictor@gmail.com, PostgreSQL mailing lists Subject: Re: BUG #15971: Behaviour of SUBSTR function depending on its arguments In-reply-to: References: <15971-965406c5a667c01a@postgresql.org> Comments: In-reply-to Ashutosh Sharma message dated "Wed, 21 Aug 2019 14:39:52 +0530" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <29035.1566395113.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 21 Aug 2019 09:45:13 -0400 Message-ID: <29036.1566395113@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Ashutosh Sharma writes: > I don't think it's a bug. I think it's just that the SUBSTR in > PostgreSQL is more SQL compliant than any other databases that you > mentioned. As per the SQL standard, if the start position is zero or > negative number, it should be adjusted to the start of the string and > not to the end of the string and that way I feel the behaviour of > SUBSTR in PostgreSQL is more SQL compliant than other databases. Yes, the standard provides no wiggle room here. The behavior of substring() with integer parameters, as specified in SQL:2008 6.29 general rule 3, is 3) If is specified, then: a) If the character encoding form of is UTF8, UTF16, or UTF32, then, in the remainder of this General Rule, the term =E2=80=9Ccharacter=E2=80=9D shall be taken to mean =E2=80=9Cunit sp= ecified by =E2=80=9D. b) Let C be the value of the , let LC be the length in characters of C, and let S be the value of the . c) If is specified, then let L be the value of and let E be S+L. Otherwise, let E be the larger of LC + 1 and S. d) If either C, S, or L is the null value, then the result of the is the null value. e) If E is less than S, then an exception condition is raised: data exception =E2=80=94 substring error. f) Case: i) If S is greater than LC or if E is less than 1 (one), then the result of the is a zero-length string. ii) Otherwise, 1) Let S1 be the larger of S and 1 (one). Let E1 be the smaller of E and LC+1. Let L1 be E1=E2=80=93S1. 2) The result of the is a character string containing the L1 characters of C starting at character number S1 in the same order that the characters appear in C. I believe our implementation does this exactly. Even if it were true that Oracle's behavior is more common than the spec's definition, it's quite unlikely that we could be talked into abandoning spec-compliant behavior to match Oracle. regards, tom lane