pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alvaro Herrera <alvherre@commandprompt.com>
To: Oliveiros C, <oliveiros.cristina@marktest.pt>
Cc: pgsql-sql@postgresql.org
Subject: Re: Is there any function to test for numeric ips?
Date: Fri, 11 Dec 2009 13:28:47 -0300
Message-ID: <20091211162847.GD30833@alvh.no-ip.org> (raw)
In-Reply-To: <E94D918326D044C69895D829038E31B2@marktestcr.marktest.pt>
References: <E94D918326D044C69895D829038E31B2@marktestcr.marktest.pt>

Oliveiros C, wrote:

> I've realized that, for ex, inet 'x.x.x.x' will fail if the input is not a numeric IP, 
> is there any simple and direct way to somewhat trap that error and convert it to a false value that can be used
> in a WHERE clause?

Yes, you can create a plpgsql function with an exception block; return
false inside the exception, true otherwise.  It looks something like

begin
  perform $1::inet;
  return true;
exception
  when invalid_something then
    return false
end;

I don't remember the exact syntax and the exception name but that should
get you started.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support



view thread (6+ messages)  latest in thread

Message-ID: <20091211162847.GD30833@alvh.no-ip.org>
Permalink:  ../20091211162847.GD30833@alvh.no-ip.org/
Also on:    postgresql.org/message-id/20091211162847.GD30833@alvh.no-ip.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: alvherre@commandprompt.com, oliveiros.cristina@marktest.pt
  Subject: Re: Is there any function to test for numeric ips?
  In-Reply-To: <20091211162847.GD30833@alvh.no-ip.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox