Re: Control PhoneNumber Via SQL

From: tango ward <tangoward15(at)gmail(dot)com>
To: raf(at)raf(dot)org
Cc: "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Control PhoneNumber Via SQL
Date: 2018-05-16 00:50:56
Message-ID: CAA6wQLJTZjHZ6tRkZgv=amYcDQeC+yndR9WyZ3eV7FrprMCc8A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi All,

Thanks for the suggestions especially the article for formatting
international phone numbers.

I also implement the suggestion of Sir Wolfgang:

cur_t.execute("""
SELECT mobilenumber,
CASE
WHEN mobilenumber ~'^0[1-9]'
THEN regexp_replace(mobilenumber, '0', '+63')
ELSE mobilenumber
END
FROM studeprofile
ORDER BY lastname
""")
x = cur_tdc.fetchone()
print x[1]

['09xxxxxxxxx', '+639xxxxxxxxx']

The output shows 2 values in a list. I only need to get the one that starts
with country code. I can do it by using x[1]. I think this has been
explained by Sir Adrian in my previous question. I'll review that.

On Wed, May 16, 2018 at 7:26 AM, <raf(at)raf(dot)org> wrote:

> hamann(dot)w(at)t-online(dot)de wrote:
>
> > >> Hi,
> > >>
> > >> I would like to know if it is possible to control the phone number in
> SQL
> > >> before inserting it to the destination DB?
> > >>
> > >> I have a model in Django:
> > >>
> > >> class BasePerson(TimeStampedModel):
> > >> phone_number = PhoneNumberField(max_length=50,
> verbose_name=_(u'phone
> > >> number'), blank=True)
> > >>
> > >> The data for phone number that I am migrating doesn't have country
> code. I
> > >> want to determine first if the number has country code in it, if it
> doesn't
> > >> then I will add the country code on the number before INSERTING it to
> the
> > >> destination database.
> > >>
> > Hi, something like
> > insert into newtable (phone, ...)
> > select case when phone ~ '^0[1-9]' then regex_replace('0', '+49',
> phone) else
> > case when phone ~ '^00' then regex_replace('00', '+', phone) else
> phone end end, ...
> > from oldtable;
> >
> > Regards
> > Wolfgang
>
> it might be better to ask this on a django forum since it sounds
> like you want django's orm to handle this. you probably just
> need to subclass PhoneNumberField so its constructor will
> reformat whatever is given to it as the phone number to be
> inserted. maybe you need a Manager class for the model (probably
> not). maybe you just need a function that takes the default
> country code or country dialling code and the phone number and
> returns what you want to insert and then always use its return
> value when assigning a value to the phone_number field. i
> suspect that subclassing PhoneNumberField is probably the best
> approach.
>
> cheers,
> raf
>
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2018-05-16 00:53:12 Re: Control PhoneNumber Via SQL
Previous Message tango ward 2018-05-16 00:47:42 Re: Control PhoneNumber Via SQL