Received: from maia.hub.org (unknown [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 126321336473 for ; Mon, 25 Oct 2010 10:14:16 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 96911-06 for ; Mon, 25 Oct 2010 13:14:08 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 60FD71336FA7 for ; Mon, 25 Oct 2010 10:14:08 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1PAMsG-00007i-Lg for pgsql-sql@postgresql.org; Mon, 25 Oct 2010 15:14:04 +0200 Received: from 212.126.222.122 ([212.126.222.122]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 25 Oct 2010 15:14:04 +0200 Received: from hari.fuchs by 212.126.222.122 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 25 Oct 2010 15:14:04 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Harald Fuchs Subject: Re: How to search for a part of a number Date: Mon, 25 Oct 2010 15:13:51 +0200 Lines: 36 Message-ID: <86tykao1o0.fsf@mgm.protecting.net> References: <4CC4D3E2.7090302@gmx.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 212.126.222.122 X-Archive: encrypt User-Agent: Gnus/5.110009 (No Gnus v0.9) Emacs/22.2 (gnu/linux) Cancel-Lock: sha1:LG3lFtoYAdHs72U38UFOXgh0s4k= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.736 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_NUMERIC_HELO=1.164, T_FILL_THIS_FORM_SHORT=0.01, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/55 X-Sequence-Number: 34706 In article <4CC4D3E2.7090302@gmx.net>, Andreas writes: > Hi, > I'm wondering if there was a clever way to find parts of a numeric > string in another table. > There is a table that holds city-codes and city-names. City-code would > be the part of a phone number that identifies the city. > Over here this code can have 2 - 5 digits. So the table would contain: > 23 ; A-City > 345 ; B-Town > 4455 ; C-Village > 632 ; D-Town > ... > I'm quite sure the numbering system is bound to be spanning a search tree. > So there are 2 tasks: > a) I'd get 445598765 in and like to know the corresponding city If the prefixes are unique: SELECT code, name FROM tbl WHERE '445598765' LIKE code || '%' If not: SELECT code, name FROM tbl WHERE '445598765' LIKE code || '%' ORDER BY length(code) DESC LIMIT 1 If your table is so large that seqscans hurt you, use the 'prefix' contrib package.