Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cxXZM-0003wo-3F for pgsql-performance@arkaria.postgresql.org; Mon, 10 Apr 2017 11:33:16 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cxXZK-00009y-TJ for pgsql-performance@arkaria.postgresql.org; Mon, 10 Apr 2017 11:33:14 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cxXZK-00006b-3q for pgsql-performance@postgresql.org; Mon, 10 Apr 2017 11:33:14 +0000 Received: from lb1-smtp-cloud2.xs4all.net ([194.109.24.21]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1cxXZG-0006Cw-SO for pgsql-performance@postgresql.org; Mon, 10 Apr 2017 11:33:12 +0000 Received: from webmail.xs4all.nl ([IPv6:2001:888:0:22:194:109:20:215]) by smtp-cloud2.xs4all.net with ESMTP id 6bZ51v00313if5201bZ5xu; Mon, 10 Apr 2017 13:33:07 +0200 Received: from mail.genj.nl ([77.242.116.162]) by webmail.xs4all.nl with HTTP (HTTP/1.1 POST); Mon, 10 Apr 2017 13:33:05 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Date: Mon, 10 Apr 2017 13:33:05 +0200 From: vinny To: "David G. Johnston" Cc: Dinesh Chandra 12108 , pgsql-performance@postgresql.org Subject: Re: Filter certain range of IP address. In-Reply-To: References: <9deae42ab489b5856f600ea514bcd51a@xs4all.nl> <705a033c9efe4a75830cc76b154e18a0@cyient.com> Message-ID: X-Sender: vinny@xs4all.nl User-Agent: XS4ALL Webmail X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On 2017-04-07 17:29, David G. Johnston wrote: > On Fri, Apr 7, 2017 at 8:18 AM, Dinesh Chandra 12108 > wrote: > >> Dear Vinny, >> >> Thanks for your valuable replay. >> >> but I need a select query, which select only that record which >> starts from IP "172.23.110" only from below table. >> >> xxx 172.23.110.175 >> yyy 172.23.110.178 >> zzz 172.23.110.177 >> aaa 172.23.110.176 >> bbb 172.23.111.180 >> ccc 172.23.115.26 > > ​SELECT ... WHERE substring(ip_addr::text, 1, 10) = '172.23.110' > > David J. > ​ While it's certainly possible to do it with a substring(), I'd strongly advise against it, for several reasons, but the main one is that it does not take into account what happens to the presentation of the IP address when cast to a string. There might be some conditions that cause it to render as '172.023.110' instead of '172.23.110' just like numbers can be rendered as '1.234,56' or '1,234.56' depending on locale, and that would break the functionality without throwing an error. Generally speaking; if you find yourself using a substring() on a datatype other than a string, you should check if there isn't an operator that already can do what you want to do. PostgreSQL has operators to do all the basic things with the datatypes it supports, so you don't have to re-invent the wheel. :-) -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance