Search This Blog

Tuesday, July 8, 2025

How Do I Use NLS_TERRITORY in Oracle APEX

Introduction
Configuring NLS_TERRITORY in an Oracle APEX application ensures that dates, numbers, and currencies are formatted according to the user's regional standards. This setting defines territory-specific behaviors such as decimal separators, date formats, and first day of the week. Applying the appropriate NLS_TERRITORY improves clarity, user comfort, and localization accuracy in multi-regional applications.

 Using NLS_TERRITORY in Oracle APEX allows you to control how dates, numbers, currency symbols, and the first day of the week are displayed throughout your application, based on the user’s regional settings. This setting helps localize content for a specific territory, improving clarity and user experience in multi-language or international environments.

Here is how to use NLS_TERRITORY in detail within an Oracle APEX application:

  1. Understand the Role of NLS_TERRITORY
    NLS_TERRITORY determines:

    • Default date format

    • Decimal and group separators

    • Currency symbol and its placement

    • The first day of the week
      Each territory (e.g., US, FR, JP) comes with its own formatting conventions. For example:

    • US → mm/dd/yyyy, dot as decimal separator

    • FR → dd/mm/yyyy, comma as decimal separator

  2. Determine Where to Set It
    In Oracle APEX, you can set NLS_TERRITORY at the session level, giving you control for each user. This is typically done using a PL/SQL process at the application or page level.

  3. Create a Dynamic Session-Level Process
    In your application, go to Shared Components > Application Processes (or use a Before Header page process), and create a process like this:

    BEGIN
      IF :APP_LANGUAGE = 'fr' THEN
        EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_TERRITORY = ''FR''';
      ELSIF :APP_LANGUAGE = 'de' THEN
        EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_TERRITORY = ''GERMANY''';
      ELSE
        EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_TERRITORY = ''AMERICA''';
      END IF;
    END;
    

    This logic adjusts the territory based on the value of an application item like :APP_LANGUAGE or a user preference stored in a custom table.

  4. Allow Users to Choose Territory (Optional)
    To provide more flexibility:

    • Create an application item (e.g., G_TERRITORY) or a user preference table.

    • Populate a dropdown list of supported territories.

    • Use this value in your initialization PL/SQL to set NLS_TERRITORY.

  5. Test Format Behavior in the UI
    After setting NLS_TERRITORY, test how your application behaves:

    • Are date pickers displaying the correct format?

    • Are numbers shown with the correct separators?

    • Is currency formatting consistent?
      These will adjust automatically based on the session-level territory setting.

  6. Avoid Overriding Territory With Format Masks
    If you use explicit format masks in item properties (like DD-MON-YYYY), they override NLS_TERRITORY. To benefit from localization, prefer using generic or dynamic format settings unless strict formatting is required.

  7. Check Current Settings (Optional Debug)
    Use the following query to view current session NLS settings:

    SELECT * FROM NLS_SESSION_PARAMETERS WHERE PARAMETER = 'NLS_TERRITORY';
    

    This helps verify that the ALTER SESSION command is working as expected.

By using NLS_TERRITORY correctly in Oracle APEX, you can align the application experience with users’ regional expectations. It enhances clarity, reduces confusion with formats, and supports international usability, making your application both professional and globally accessible.

 

There are multiple ways to configure NLS_TERRITORY within Oracle APEX:

1. Setting NLS_TERRITORY at the Database Level

If you want the NLS_TERRITORY setting to apply globally across the entire database, you can modify it using the ALTER SESSION command.

ALTER SESSION SET NLS_TERRITORY = 'GERMANY';

This will update the session-specific settings for numeric formatting, date formats, and currency symbols according to German standards.

To check the current NLS_TERRITORY setting in the session, run:

SELECT VALUE FROM NLS_SESSION_PARAMETERS WHERE PARAMETER = 'NLS_TERRITORY';

2. Setting NLS_TERRITORY at the Application Level

Within Oracle APEX, you can define NLS_TERRITORY settings at the Application Level.

  1. Open App Builder and select your application.

  2. Navigate to Shared Components.

  3. Click Globalization Attributes.

  4. In the Date Format section, set the desired NLS_TERRITORY.

  5. Click Apply Changes.

This ensures that the selected country format applies throughout the application.


Dynamically Changing NLS_TERRITORY

3. Setting NLS_TERRITORY Per User

If you want different users to have different NLS_TERRITORY settings based on their location, you can modify it dynamically during login.

Create a Before Header Process on the Login Page to set the NLS_TERRITORY dynamically:

BEGIN

    IF APEX_UTIL.GET_SESSION_LANG = 'fr' THEN

        EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_TERRITORY = ''FRANCE''';

    ELSIF APEX_UTIL.GET_SESSION_LANG = 'de' THEN

        EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_TERRITORY = ''GERMANY''';

    ELSE

        EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_TERRITORY = ''UNITED STATES''';

    END IF;

END;

This ensures that users get the correct regional settings based on their language preference.


Using NLS_TERRITORY in SQL Queries

Once the NLS_TERRITORY setting is applied, it automatically affects how dates, numbers, and currency values are formatted in SQL queries.

Example 1: Formatting Dates Based on NLS_TERRITORY

SELECT TO_CHAR(SYSDATE, 'DD-MON-YYYY') FROM DUAL;

If NLS_TERRITORY is set to "GERMANY", this will return dates in the DD.MM.YYYY format instead of the US MM/DD/YYYY format.

Example 2: Formatting Numbers Based on NLS_TERRITORY

SELECT TO_CHAR(1234567.89, '999G999G999D99') FROM DUAL;

  • In "UNITED STATES", the output will be 1,234,567.89.

  • In "FRANCE", it will be 1 234 567,89.

  • In "GERMANY", it will be 1.234.567,89.

Example 3: Formatting Currency Based on NLS_TERRITORY

SELECT TO_CHAR(1000, 'L999G999D99') FROM DUAL;

  • In "UNITED STATES", the output will be $1,000.00.

  • In "UNITED KINGDOM", it will be £1,000.00.

  • In "JAPAN", it will be ¥1,000.


Applying NLS_TERRITORY in PL/SQL

If you need to apply NLS_TERRITORY settings inside a PL/SQL block, you can use:

BEGIN

    EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_TERRITORY = ''ITALY''';

END;

To get the current NLS_TERRITORY value inside a PL/SQL block:

DECLARE

    v_territory VARCHAR2(50);

BEGIN

    SELECT VALUE INTO v_territory 

    FROM NLS_SESSION_PARAMETERS 

    WHERE PARAMETER = 'NLS_TERRITORY';

    

    DBMS_OUTPUT.PUT_LINE('Current Territory: ' || v_territory);

END;


Best Practices for Using NLS_TERRITORY

  • Set NLS_TERRITORY early in a session to avoid inconsistencies in date or number formatting.

  • Use session-based settings instead of global database settings to allow different users to have different formats.

  • Test different locales by changing the NLS_TERRITORY value to ensure proper formatting.

  • Store user preferences in a database table and apply them dynamically when a user logs in.

Example:

DECLARE

    v_user_territory VARCHAR2(50);

BEGIN

    SELECT user_territory INTO v_user_territory FROM user_preferences WHERE user_id = :APP_USER;

    EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_TERRITORY = ''' || v_user_territory || '''';

END;

This way, users can choose their preferred settings, and the application will automatically adjust.


Implementing NLS_TERRITORY in Oracle APEX ensures that numeric, currency, and date formats are properly displayed according to a user’s regional preferences. By setting NLS_TERRITORY at the database, application, or session level, and using it in SQL and PL/SQL queries, you can build a globally adaptable application.

This enhances the user experience by allowing automatic formatting of data based on local conventions, ensuring better readability and usability for international users.


NLS_LANGUAGE and NLS_TERRITORY determine the default presentation of numbers, dates, and currencies.


Language Name

Language Code

NLS_LANGUAGE

NLS_TERRITORY

Afrikaans

af

ENGLISH

SOUTH AFRICA

Arabic

ar

ARABIC

UNITED ARAB EMIRATES

Arabic (Algeria)

ar-dz

ARABIC

ALGERIA

Arabic (Bahrain)

ar-bh

ARABIC

BAHRAIN

Arabic (Egypt)

ar-eg

EGYPTIAN

EGYPT

Arabic (Iraq)

ar-iq

ARABIC

IRAQ

Arabic (Jordan)

ar-jo

ARABIC

JORDAN

Arabic (Kuwait)

ar-kw

ARABIC

KUWAIT

Arabic (Lebanon

ar-lb

ARABIC

LEBANNON

Arabic (Libya)

ar-ly

ARABIC

LIBYA

Arabic (Morocco)

ar-ma

ARABIC

MOROCCO

Arabic (Oman)

ar-om

ARABIC

OMAN

Arabic (Qatar)

ar-qa

ARABIC

QATAR

Arabic (Saudi Arabia)

ar-sa

ARABIC

SAUDI ARABIA

Arabic (Syria)

ar-sy

ARABIC

SYRIA

Arabic (Tunisia)

ar-tn

ARABIC

TUNISIA

Arabic (U.A.E.)

ar-ae

ARABIC

UNITED ARAB EMIRATES

Arabic (YEMEN)

ar-ye

ARABIC

YEMEN

Assamese

as

ASSAMESE

INDIA

Basque

eu

FRENCH

FRANCE

Belarusian

be

RUSSIAN

RUSSIA

Bengali

bn

BANGLA

BANGLADESH

Bulgarian

bg

BULGARIAN

BULGARIA

Catalan

ca

CATALAN

CATALONIA

Chinese

zh

SIMPLIFIED CHINESE

CHINA

Chinese (China)

zh-cn

SIMPLIFIED CHINESE

CHINA

Chinese (Hong Kong SAR)

zh-hk

TRADITIONAL CHINESE

HONG KONG

Chinese (Macau SAR)

zh-mo

TRADITIONAL CHINESE

HONG KONG

Chinese (Singapore)

zh-sg

SIMPLIFIED CHINESE

SINGAPORE

Chinese (Taiwan)

zh-tw

TRADITIONAL CHINESE

TAIWAN

Croatian

hr

CROATIAN

CROATIA

Czech

cs

CZECH

CZECH REPUBLIC

Danish

da

DANISH

DENMARK

Dutch (Belgium)

nl-be

DUTCH

BELGIUM

Dutch (Netherlands)

nl

DUTCH

THE NETHERLANDS

English

en

AMERICAN

AMERICA

English (Australia)

en-au

ENGLISH

AUSTRALIA

English (Belize)

en-bz

ENGLISH

UNITED KINGDOM

English (Canada)

en-ca

ENGLISH

CANADA

English (Ireland)

en-ie

ENGLISH

IRELAND

English (Jamaica)

en-jm

ENGLISH

UNITED KINGDOM

English (New Zealand)

en-nz

ENGLISH

NEW ZEALAND

English (Philippines)

en-ph

ENGLISH

PHILIPPINES

English (South Africa)

en-za

ENGLISH

SOUTH AFRICA

English (Trinidad)

en-tt

ENGLISH

UNITED KINGDOM

English (United Kingdom)

en-gb

ENGLISH

UNITED KINGDOM

English (United States)

en-us

AMERICAN

AMERICA

English (Zimbabwe)

en-zw

ENGLISH

UNITED KINGDOM

Estonian

et

ESTONIAN

ESTONIA

Faeroese

fo

ENGLISH

UNITED KINGDOM

Farsi

fa

ENGLISH

UNITED KINGDOM

Finnish

fi

FINNISH

FINLAND

French (Belgium)

fr-be

FRENCH

BELGIUM

French (Canada)

fr-ca

CANADIAN FRENCH

CANADA

French (France)

fr

FRENCH

FRANCE

French (Luxembourg)

fr-lu

FRENCH

LUXEMBOURG

French (Monaco)

fr-mc

FRENCH

FRANCE

French (Switzerland)

fr-ch

FRANCH

SWITZERLAND

FYRO Macedonian

mk

MACEDONIAN

FYR MACEDONIA

Gaelic

gd

ENGLISH

UNITED KINGDOM

Galician

gl

SPANISH

SPAIN

German (Austria)

de-at

GERMAN

AUSTRIA

German (Germany)

de

GERMAN

GERMANY

German (Liechtenstein)

de-li

GERMAN

GERMANY

German (Luxemgourg)

de-lu

GERMAN

LUXEMBOURG

German (Switzerland)

de-ch

GERMAN

SWITZERLAND

Greek

el

GREEK

GREECE

Gujarati

gu

GUJARATI

INDIA

Hebrew

he

HEBREW

ISRAEL

Hindi

hi

HINDI

INDIA

Hungarian

hu

HUNGARIAN

HUNGARY

Icelandic

is

ICELANDIC

ICELAND

Indonesian

id

INDONESIAN

INDONESIA

Italian (Italy)

it

ITALIAN

ITALY

Italian (Switzerland)

it-ch

ITALIAN

SWITZERLAND

Japanese

ja

JAPANESE

JAPAN

Kannada

kn

KANNADA

INDIA

Kazakh

kk

CYRILLIC KAZAKH

KAZAKHSTAN

Konkani

kok

KOREAN

KOREA

Korean

ko

KOREAN

KOREA

Kyrgyz

kz

RUSSIAN

RUSSIA

Latvian

lv

LATVIAN

LATVIA

Lithuanian

lt

LITHUANIAN

LITHUANIANA

Malay (Malaysia)

ms

MALAY

MALAYSIA

Malayalam

ml

MALAYALAM

INDIA

Maltese

mt

ENGLISH

UNITED KINGDOM

Marathi

mr

ENGLISH

INDIA

Nepali (India)

ne

ENGLISH

UNITED KINGDOM

Norwegian (Bokmal)

nb-no

NORWEGIAN

NORWAY

Norwegian (Bokmal)

no

NORWEGIAN

NORWAY

Norwegian (Nynorsk)

nn-no

NORWEGIAN

NORWAY

Oriya

or

ORIYA

INDIA

Polish

pl

POLISH

POLAND

Portuguese (Brazil)

pt-br

BRAZILIAN PORTUGUESE

BRAZIL

Portuguese (Portugal)

pt

PORTUGUESE

PORTUGAL

Punjabi

pa

PUNJABI

INDIA

Romanian

ro

ROMANIAN

ROMANIA

Russian

ru

RUSSIAN

RUSSIA

Russian (Moldova)

ru-md

RUSSIAN

RUSSIA

Serbia

sr

CYRILLIC SERBIAN

SERBIA AND MONTENEGRO

Slovak

sk

SLOVAK

SLOVAKIA

Slovenian

sl

SLOVENIAN

SLOVENIA

Spanish (Argentina)

es-ar

LATIN AMERICAN SPANISH

ARGENTINA

Spanish (Bolivia)

es-bo

LATIN AMERICAN SPANISH

ARGENTINA

Spanish (Chile)

es-cl

LATIN AMERICAN SPANISH

CHILE

Spanish (Colombia)

ec-co

LATIN AMERICAN SPANISH

COLOMBIA

Spanish (Costa Rica)

es-cr

LATIN AMERICAN SPANISH

COSTA RICA

Spanish (Dominican Republic)

es-do

LATIN AMERICAN SPANISH

PUERTO RICO

Spanish (Ecuador)

es-ec

LATIN AMERICAN SPANISH

ECUDOR

Spanish (El Salvador)

es-sv

LATIN AMERICAN SPANISH

EL SALVADOR

Spanish (Guatemala)

es-gt

LATIN AMERICAN SPANISH

GUATEMALA

Spanish (Honduras)

es-hn

LATIN AMERICAN SPANISH

GUATEMALA

Spanish (Mexico)

es-mx

MEXICAN SPANISH

MEXICO

Spanish (Nicaragua)

es-ni

LATIN AMERICAN SPANISH

Nicaragua

Spanish (Panama)

es-pa

LATIN AMERICAN SPANISH

Panama

Spanish (Paraguay)

es-py

LATIN AMERICAN SPANISH

ARGENTINA

Spanish (Peru)

es-pe

LATIN AMERICAN SPANISH

PERU

Spanish (Puerto Rico)

es-pr

LATIN AMERICAN SPANISH

PUERTO RICO

Spanish (Traditional Sort)

es

LATIN AMERICAN SPANISH

SPAIN

Spanish (United States)

es-us

LATIN AMERICAN SPANISH

AMERICAN

Spanish (Uruguay)

es-uy

LATIN AMERICAN SPANISH

ARGENTINA

Spanish (Venezuela)

es-ve

LATIN AMERICAN SPANISH

VENEZUELA

Swedish

sv

SWEDISH

SWEDEN

Swedish

sv-fi

SWEDISH

FINLAND

Tamil

ta

TAMIL

INDIA

Telugu

te

TELUGU

INDIA

Thai

th

THAI

THAILAND

Turkish

tr

TURKISH

TURKEY

Ukrainian

uk

UKRAINIAN

UKRAINE

Urdu

ur

ENGLISH

UNITED KINGDOM

Uzbek

uz

LATIN UZBEK

UZBEKISTAN

Vietnamese

vi

VIETNAMESE

VIETNAM


Conclusion
By configuring NLS_TERRITORY at the session level within your APEX application, you align data presentation with regional expectations. Whether based on user preferences, browser language, or application logic, setting this parameter correctly provides users with a more intuitive and culturally consistent experience across your app.

 

No comments:

Post a Comment

HOW DO I SET A HOME PAGE

 Setting a home page in Oracle APEX is an essential step in defining the default landing page for your application. The home page serves as ...