X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 2D34152819 for ; Fri, 17 Jun 2005 09:07:01 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 02848-01 for ; Fri, 17 Jun 2005 12:06:58 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id B8DA452828 for ; Fri, 17 Jun 2005 09:06:56 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j5HC6ucf069833 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 17 Jun 2005 06:06:59 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j5HC6ud1072583 for ; Fri, 17 Jun 2005 06:06:56 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j5HC6usp072582 for pgsql-sql@postgresql.org; Fri, 17 Jun 2005 06:06:56 -0600 (MDT) (envelope-from mfuhr) Date: Fri, 17 Jun 2005 06:06:56 -0600 From: Michael Fuhr To: pgsql-sql@postgresql.org Subject: Re: substr or char_length problem Message-ID: <20050617120656.GA72339@winnie.fuhr.org> References: <014a01c57323$90b72b20$b8e58bd5@cerit> <20050617102528.GD15166@webserv.wug-glas.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050617102528.GD15166@webserv.wug-glas.de> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.005 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200506/218 X-Sequence-Number: 21819 On Fri, Jun 17, 2005 at 12:25:28PM +0200, Andreas Kretschmer wrote: > am 17.06.2005, um 13:01:15 +0300 mailte Fatih Cerit folgendes: > > > > I have a problem with function substr or char_length or both. I guery A2 > > and it works fine. But sometimes gives 'ERROR: negative substring length > > not allowed'. When I test many many times with diffrent values, never gives > > error. Sample table and query below. > > > > A1 A2 > > ------------------- > > 1 1957 > > 2 197 > > 3 19 > > 4 > > 5 NULL > > 6 1 > > 7 195 > > > > Select * from tbl_xxx where > > tbl_xxx.A2=substr('196895588454554545454',0,char_length(tbl_xxx.A2)+1); > > Perhaps because char_length() returns NULL and this is a invalid value > for substr(). Use coalesce(): substr() is marked STRICT, also known as RETURNS NULL ON NULL INPUT, so it should simply return NULL if the length is NULL: SELECT oid::regprocedure, proisstrict FROM pg_proc WHERE proname = 'substr'; oid | proisstrict -------------------------------+------------- substr(bytea,integer) | t substr(text,integer) | t substr(bytea,integer,integer) | t substr(text,integer,integer) | t (4 rows) SELECT substr('196895588454554545454', 0, NULL + 1) IS NULL; ?column? ---------- t (1 row) The error "negative substring length not allowed" implies that the length being passed is negative. Since the query adds 1 to the return value of char_length(), that implies that char_length() is returning a value <= -2. I don't know what could cause that short of a bug in the backend. Or am I missing something? I couldn't duplicate the error with the given example -- is that the real data and query or just a contrived example that doesn't actually fail? What version of PostgreSQL are you using? What encoding? What OS and version? What are the results of the following query? SELECT a1, char_length(a2), a2 FROM tbl_xxx WHERE char_length(a2) < 0; Could you post a self-contained test case, that is, a complete list of SQL statements that somebody could load into an empty database to reproduce the problem? -- Michael Fuhr http://www.fuhr.org/~mfuhr/