Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VWsJx-0004yI-Q4 for pgsql-sql@arkaria.postgresql.org; Thu, 17 Oct 2013 18:29:18 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VWsJx-0003UJ-Aa for pgsql-sql@arkaria.postgresql.org; Thu, 17 Oct 2013 18:29:17 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VWsJw-0003UD-J4 for pgsql-sql@postgresql.org; Thu, 17 Oct 2013 18:29:16 +0000 Received: from mbx.knossos.net.nz ([202.160.48.10]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VWsJn-0006F2-2o for pgsql-sql@postgresql.org; Thu, 17 Oct 2013 18:29:15 +0000 Received: from [10.1.1.3] (60-234-150-59.bitstream.orcon.net.nz [60.234.150.59]) (authenticated bits=0) by mbx.knossos.net.nz (8.14.4/8.14.4) with ESMTP id r9HIT05g032600 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Fri, 18 Oct 2013 07:29:01 +1300 Message-ID: <52602BF9.7020403@archidevsys.co.nz> Date: Fri, 18 Oct 2013 07:27:05 +1300 From: Gavin Flower Organization: ArchiDevSys User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.0 MIME-Version: 1.0 To: Gary Stainburn , "pgsql-sql@postgresql.org" Subject: Re: Advice - indexing on varchar fields where only last x characters known References: <201310171220.44550.gary.stainburn@ringways.co.uk> In-Reply-To: <201310171220.44550.gary.stainburn@ringways.co.uk> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 0.8 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 18/10/13 00:20, Gary Stainburn wrote: > I have a problem with a field that appears on a number of my tables. > > The field is the Vehicle Identification Number. Every vehicle has one and it > uniquely identifies that vehicle. > > Traditionally this was a 11 character string but a number of years ago was > extended to 17 characters by adding a 6 character prefix. > > > The problem that I have is that these VIN numbers are provided by a number of > data systems including manufacturer feeds, logistics companies as well as > internal systems. Some use the full 17 character string while others only use > the last 11. > > On top of this, my users are used to only having to type the last 6 characters > for speed and usability reasons. > > However, it means that every time I'm trying to connect various tables up > using foreign keys or doing searches I have to make allowences for this which > means I'm using things like substring, like, regex etc. all of which are very > slow. > > Can anyone suggest a better / more efficient way of handling these. > > Gary > > Use 2 fields, one for the 6 character prefix, and the other for the original 11 digits. Search for the 6 character prefix, or a null prefix AND the first 6 characters of the 11 digit field. It might be better to have a string for the prefix and make it blank rather than null, when nothing is entered there. Cheers, Gavin -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql