Re: How to split up phone numbers?

From: Andy Colson <andy(at)squeakycode(dot)net>
To: Andreas <maps(dot)on(at)gmx(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to split up phone numbers?
Date: 2012-02-20 15:54:11
Message-ID: 4F426CA3.7050508@squeakycode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 02/20/2012 08:49 AM, Andreas wrote:
> Hi,
> is there a way to split up phone numbers?
> I know that's a tricky topic and it depends on the national phone number format.
> I'm especially interested in a solution for Germany, Swizerland and Austria.
>
> I've got everything in a phone number column that makes hardly sense like:
> +49432156780
> 0049 4321 5678 0
> 04321/5678-0
> and so on...
> Those 3 samples are actually the same number in different notations.
>
> Aim would be to get a normalized number split up in 4 seperate columns
> nr_nation
> nr_city
> nr_main
> nr_individual
>
> so I end up with
> 49 4321 5678 0 for central
> 49 4321 5678 42 for Mr. Smith
>
> Is this doable?
>
> It would be a start to at least split off nr_nation and nr_city.
>

I would do it in multiple passes. Trim everything out (spaces, slashes, etc) to try and get a constant number, then use the length to determin the different types of numbers, then use substring to pull out the parts.

-- for shorter phone numbers
select '49' as nr_nation,
substring(phone from 1 for 4) as nr_city,
etc...
where length( regexp_replace(phone, '[^\d]', '', 'g') ) = 10
and nr_nation is null;

-- for longer phone numbers
select substring(phone from 1 for 2) as nr_nation,
substring(phone from 3 for 4) as nr_city,
etc...
where length( regexp_replace(phone, '[^\d]', '', 'g') ) = 12
and nr_nation is null;

-- etc

-Andy

In response to

Browse pgsql-general by date

  From Date Subject
Next Message mgould 2012-02-20 17:07:18 Re: Question on Rules
Previous Message Andreas 2012-02-20 14:49:19 How to split up phone numbers?