Received: from maia.hub.org (unknown [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id CE6FF1337B50 for ; Sun, 24 Oct 2010 21:48:23 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 04211-08 for ; Mon, 25 Oct 2010 00:48:16 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail.gmx.net (mailout-de.gmx.net [213.165.64.23]) by mail.postgresql.org (Postfix) with SMTP id 6E9C71336473 for ; Sun, 24 Oct 2010 21:48:15 -0300 (ADT) Received: (qmail invoked by alias); 25 Oct 2010 00:48:14 -0000 Received: from mue-88-130-45-085.dsl.tropolys.de (EHLO [192.168.0.113]) [88.130.45.85] by mail.gmx.net (mp061) with SMTP; 25 Oct 2010 02:48:14 +0200 X-Authenticated: #14269776 X-Provags-ID: V01U2FsdGVkX19PcbF6hTBP5dfR6Lr80ygc7zJEaaw17VQZafFVBI 5qVmHt8eR/0KyY Message-ID: <4CC4D3E2.7090302@gmx.net> Date: Mon, 25 Oct 2010 02:48:34 +0200 From: Andreas User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.11) Gecko/20101013 Thunderbird/3.1.5 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: How to search for a part of a number Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, T_FILL_THIS_FORM_SHORT=0.01, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/54 X-Sequence-Number: 34705 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 b) I'd like to get the number nicely formatted : 4 4 55987-65 --> 4455 / 98765 Obviously one could do it on the client side. As I dont know how many digits in the input are relevant, I need to try the shortest code first. 1) search for 44 2) add 5 and search for 445 3) add the 2nd. 5 and search for 4455 BINGO Is there a way to do it within the db?