X-Original-To: pgsql-sql@postgresql.org Received: from localhost (developer.postgresql.org [64.117.224.193]) by developer.postgresql.org (Postfix) with ESMTP id 02B47925670 for ; Tue, 27 May 2003 04:55:46 -0400 (EDT) Received: from developer.postgresql.org ([64.117.224.193]) by localhost (developer.postgresql.org [64.117.224.193:10024]) (amavisd-new) with ESMTP id 99601-10 for ; Tue, 27 May 2003 04:55:44 -0400 (EDT) Received: from fzkmail2.fzk.de (fzkmail2.fzk.de [141.52.27.52]) by developer.postgresql.org (Postfix) with SMTP id BFF2592565F for ; Tue, 27 May 2003 04:55:43 -0400 (EDT) Received: FROM resy5.fzk.de BY fzkmail2.fzk.de ; Tue May 27 10:57:15 2003 +0200 Received: œby rodos.fzk.de with ESMTP (8.8.6 (PHNE_17135)/8.7.1) id KAA27979; Tue, 27 May 2003 10:55:41 +0200 (METDST) Message-ID: <3ED3280C.2090601@rodos.fzk.de> Date: Tue, 27 May 2003 10:55:40 +0200 From: Christoph Haller User-Agent: Mozilla/5.0 (X11; U; HP-UX 9000/780; en-US; rv:1.3) Gecko/20030321 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Cc: tlqmail@yahoo.com Subject: Re: Using UPPER and TRIM (INDEX usage) Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/503 X-Sequence-Number: 13668 > > How do I use "TRIM" and "UPPER" both in the SQL statement and still use the index. > > I created an index on myTable in the following way > > CREATE INDEX index_fname_myTable ON myTable USING btree (upper(fname)); > > Now the SQL I used was > > > SELECT * FROM myTable where upper(trim(fname))= 'JOHN':: character varying > > The postgresql doesnt use the index on fname in the above case. > > But if I use only "UPPER" in the SQL statement, the postgresql uses the index. > For eg. SELECT * FROM myTable where upper(fname)= 'JOHN':: character varying And what happens if you re-write it as SELECT * FROM myTable where trim(upper(fname))= 'JOHN':: character varying Alternatively try CREATE FUNCTION uppertrim (character varying) returns character varying as ' select upper(trim($1)); ' LANGUAGE SQL IMMUTABLE ; and then CREATE INDEX index_fname2_myTable ON myTable USING btree (uppertrim(fname)); Regards, Christoph