Home » SQL & PL/SQL » SQL & PL/SQL » Oracle Function to get ISO Country Code (11g)
icon5.gif  Oracle Function to get ISO Country Code [message #626491] Tue, 28 October 2014 07:50 Go to next message
suddhasatwa_oracle
Messages: 24
Registered: January 2014
Junior Member
Hello All

I have a requirement as part of which I would have to get the ISO Country Code from the ISD Code.

Is there a function in Oracle to do so, or, is there any customer available script which anyone can suggest?

Many thanks in anticipation,

Best Regards,
Suddhasatwa
Re: Oracle Function to get ISO Country Code [message #626494 is a reply to message #626491] Tue, 28 October 2014 07:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

You have to have a table for this.
Wikipedia give you both ISD code and ISO code for each country, just merge them.
http://en.wikipedia.org/wiki/List_of_country_calling_codes
http://en.wikipedia.org/wiki/ISO_3166-1

[Edit: change French to English url]

[Updated on: Tue, 28 October 2014 07:59]

Report message to a moderator

Re: Oracle Function to get ISO Country Code [message #626497 is a reply to message #626494] Tue, 28 October 2014 08:03 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
https://community.oracle.com/thread/3624062
Re: Oracle Function to get ISO Country Code [message #626498 is a reply to message #626491] Tue, 28 October 2014 08:03 Go to previous messageGo to next message
EdStevens
Messages: 1377
Registered: September 2013
Senior Member
https://community.oracle.com/thread/3624062
Re: Oracle Function to get ISO Country Code [message #626501 is a reply to message #626494] Tue, 28 October 2014 08:28 Go to previous messageGo to next message
suddhasatwa_oracle
Messages: 24
Registered: January 2014
Junior Member
Hello Michael

Thanks for your reply.

I have a table now in which I have the ISD Code and the ISO Country Code.

Given the phone number is 12 digit in my data, I am trying to figure out how to write an optimal code to check the ISO Country code when the ISD Code is more than 2 characters long.

I have a function in place now, which can tell me the ISO Country code if ISD code is the first 2 characters of the phone number - this was simple to implement.

However, if it is 3 characters long, e.g.,351 for Portugal, then my function does not return the correct values, and in some cases multiple values.

Could you kindly advise how I can tackle this situation here?

Regards,
Suddhasatwa
Re: Oracle Function to get ISO Country Code [message #626502 is a reply to message #626491] Tue, 28 October 2014 08:30 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

You say in the OTN thread that you already have this table.
Pleas post a description of the table and a dump of it so that people searching for this has not to waste to recreate the same thing.

Re: Oracle Function to get ISO Country Code [message #626503 is a reply to message #626501] Tue, 28 October 2014 08:31 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Post a dump of the table and some data to test and we can work with them to find how to fit your needs.

Re: Oracle Function to get ISO Country Code [message #626504 is a reply to message #626503] Tue, 28 October 2014 08:33 Go to previous messageGo to next message
suddhasatwa_oracle
Messages: 24
Registered: January 2014
Junior Member
Here is my table:

CREATE TABLE COUNTRY(
COUNTRY_CODE NUMBER NOT NULL,
COUNTRY_NAME VARCHAR2(255) NOT NULL,
ISO_COUNTRY CHAR(10) NOT NULL,
CURRENCY_CODE VARCHAR2(3) NOT NULL,
TIMEZONE_OFFSET NUMBER NOT NULL,
prefix_country_code varchar2(5),
ISO_CODE VARCHAR2(3)
);

Here's a few lines of data for above table:

INSERT INTO COUNTRY VALUES (0,'Belguim','BE','EUR', 0, '32','BE');
INSERT INTO COUNTRY VALUES (1,'Switzerland','CH','EUR', 0, '41','CH');
INSERT INTO COUNTRY VALUES (2,'Germany','DE','EUR', 0, '49','DE');
INSERT INTO COUNTRY VALUES (3,'Spain','ES','EUR', 0, '34','ES');
INSERT INTO COUNTRY VALUES (4,'France','FR','EUR', 0, '33','FR');
INSERT INTO COUNTRY VALUES (5,'UK','GB','GBP',-1, '44','GB');
INSERT INTO COUNTRY VALUES (6,'Greece','GR','EUR', 2, '30','GR');
INSERT INTO COUNTRY VALUES (7,'Hungary','HU','HUF', 0, '36','HU');
INSERT INTO COUNTRY VALUES (8,'Ireland','IE','EUR', -1, '353','IE');
INSERT INTO COUNTRY VALUES (9,'Italy','IT','EUR', 0, '39','IT');
INSERT INTO COUNTRY VALUES (10,'Netherlands','NL','EUR', 0, '31','NL');
INSERT INTO COUNTRY VALUES (11,'Poland','PL','PLN', 0, '48','PL');
INSERT INTO COUNTRY VALUES (12,'Portugal','PT','EUR', -1, '351','PT');
COMMIT;

As seen above, there are instances where the ISD code is 3 characters as well.
Re: Oracle Function to get ISO Country Code [message #626506 is a reply to message #626504] Tue, 28 October 2014 08:43 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
The problem is that you define country_code as string not as NUMBER
Re: Oracle Function to get ISO Country Code [message #626510 is a reply to message #626504] Tue, 28 October 2014 08:55 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

This is not what I asked.
And fix your table as BlackSwan said.

And please format your post as explained in How to use [code] tags and make your code easier to read.

Re: Oracle Function to get ISO Country Code [message #626512 is a reply to message #626510] Tue, 28 October 2014 09:01 Go to previous messageGo to next message
suddhasatwa_oracle
Messages: 24
Registered: January 2014
Junior Member
Thanks to all

I have got the solution to the problem.
Re: Oracle Function to get ISO Country Code [message #626514 is a reply to message #626512] Tue, 28 October 2014 09:02 Go to previous messageGo to next message
suddhasatwa_oracle
Messages: 24
Registered: January 2014
Junior Member
It was basically writing a funtion with couple of sub queries to check for the ISD Code upto 3 characters and match it with the base/reference table.
Re: Oracle Function to get ISO Country Code [message #626515 is a reply to message #626514] Tue, 28 October 2014 09:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Post the final query to complete the topic.
And post a dump of your table.

Re: Oracle Function to get ISO Country Code [message #626596 is a reply to message #626514] Wed, 29 October 2014 12:27 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

No answer?

Should we think you ask us to spend our time for you and help you but don't want to help others?
Should we think you ask us to share with you what we know and have but you don't want to share with us what you have?

Re: Oracle Function to get ISO Country Code [message #626632 is a reply to message #626596] Thu, 30 October 2014 02:42 Go to previous messageGo to next message
suddhasatwa_oracle
Messages: 24
Registered: January 2014
Junior Member
Apologies, I was on leave

This is the SQL Query which I used inside a function I created, which uses phone number as the input (variable: v_msisdn) and returns the country code from the base table.

    SELECT
      CASE
        WHEN SUBSTR(v_msisdn , 1, 1) IN
          (SELECT c.prefix_country_code
          FROM country c
          WHERE c.prefix_country_code= SUBSTR(v_msisdn , 1, 1)
          )
        THEN
          (SELECT c.iso_country
          FROM country c
          WHERE c.prefix_country_code=SUBSTR(v_msisdn , 1, 1)
          )
        WHEN SUBSTR(v_msisdn , 1, 2) IN
          (SELECT c.prefix_country_code
          FROM country c
          WHERE c.prefix_country_code= SUBSTR(v_msisdn , 1, 2)
          )
        THEN
          (SELECT c.iso_country
          FROM country c
          WHERE c.prefix_country_code=SUBSTR(v_msisdn , 1, 2)
          )
        WHEN SUBSTR(v_msisdn , 1, 3) IN
          (SELECT c.prefix_country_code
          FROM country c
          WHERE c.prefix_country_code= SUBSTR(v_msisdn , 1, 3)
          )
        THEN
          (SELECT c.iso_country
          FROM country c
          WHERE c.prefix_country_code=SUBSTR(v_msisdn , 1, 3)
          )
        ELSE 'N/A'
      END AS ISO_COUNTRY
    INTO v_return_code
    FROM dual;


This solved the problem.
Re: Oracle Function to get ISO Country Code [message #626637 is a reply to message #626632] Thu, 30 October 2014 03:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Thank you.
Note that this is not the most efficient way to solve the problem.
Can you post a dump of your table.

[Updated on: Thu, 30 October 2014 03:17]

Report message to a moderator

Re: Oracle Function to get ISO Country Code [message #626648 is a reply to message #626637] Thu, 30 October 2014 05:39 Go to previous messageGo to next message
suddhasatwa_oracle
Messages: 24
Registered: January 2014
Junior Member
Should I post the CREATE TABLE and INSERT commands for the table I have created? Or should I take an Oracle EXPORT and upload it here?
I know, there can be better solutions, but I needed to have this quickly, and this is the best I can come up with, in limited time. Sad
Re: Oracle Function to get ISO Country Code [message #626654 is a reply to message #626648] Thu, 30 October 2014 06:04 Go to previous message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Put a dump file. Old export or Data Pump as you want.
You can zip it to decrease the size.
If the site does not allow you upload a zip file just change the extension to txt.
Thanks


Previous Topic: Oracle GTT vs SQL Temporary Table
Next Topic: How to use PL/SQL Collection Type in TABLE function(how to convert PL/SQL type to SQL Object Type)
Goto Forum:
  


Current Time: Mon Sep 01 11:04:38 CDT 2025