Introduction

Many times, when you have an Oracle application and you have to support special characters like ö,ä,ü,é,è or currency symbols (e.g. ), you encounter problems with proper display. Mostly, this problem is caused by improper setting of NLS_LANG value.

NLS_LANG sets the language and territory used by the client application and the database server. It also sets the client's character set, which is the character set for data entered or displayed by a client program.

Character Set of Database

When an Oracle Database is created, the DBA has to specify the CHARACTER SET and the NATIONAL CHARACTER SET.

Nowadays, the default values are:

  • AL32UTF8 for CHARACTER SET and
  • AL16UTF16 for NATIONAL CHARACTER SET

These Database character sets define which characters (in which format) can be stored in CHARCLOBVARCHAR2resp. in NCHARNCLOBNVARCHAR2 column. On an existing database, you can query the values with:

Hide   Copy Code
SELECT *
FROM NLS_DATABASE_PARAMETERS
WHERE PARAMETER LIKE '%CHARACTERSET'; PARAMETER VALUE
==========================================
NLS_CHARACTERSET AL32UTF8
NLS_NCHAR_CHARACTERSET AL16UTF16 2 row(s) selected.

The database character sets do not define if and how charaters are displayed in your client application!

Some Facts of NLS_LANG

Format of NLS_LANG definition is NLS_LANG = LANGUAGE_TERRITORY.CHARSET

All components of the NLS_LANG definition are optional; any item that is not specified uses its default value. If you specify territory or character set, then you must include the preceding delimiter [underscore (_) for territory, period (.) for character set]. Otherwise, the value is parsed as a language name.

Following definitions are all valid:

  • NLS_LANG=.WE8ISO8859P1
  • NLS_LANG=_GERMANY
  • NLS_LANG=AMERICAN
  • NLS_LANG=ITALIAN_.WE8MSWIN1252
  • NLS_LANG=_BELGIUM.US7ASCII

If NLS_LANG value is not provided, then Oracle defaults it to AMERICAN_AMERICA.US7ASCII.

LANGUAGE and TERRITORY set the default value for many other NLS Parameters, see this table to get an overview. CHARSET is used to let Oracle know what character set you are using on the client side, so Oracle can do the proper conversion. Setting the LANGUAGE and TERRITORY parameters of NLS_LANG has nothing to do with the ability to store characters in a database. Here, you see a list of available LanguagesTerritoriesand Character Sets.

You can change the language and territory of your session by:

Hide   Copy Code
ALTER SESSION SET NLS_LANGUAGE = '...';
respective
ALTER SESSION SET NLS_TERRITORY = '...';

However, you cannot change your client charset with any SQL command, it is set only by the NLS_LANG value.

Some setting can be explicitly set in SQL functions, for example:

Hide   Copy Code
SELECT TO_CHAR(SYSDATE, 'DD Month', 'NLS_DATE_LANGUAGE = FRENCH')
FROM dual;

other can not, e.g.:

Hide   Copy Code
SELECT TRUNC(SYSDATE, 'DY', 'NLS_TERRITORY = AMERICA') AS FIRST_DAY_OF_WEEK
FROM dual;

does not work.

You cannot query your client charset by any dictionary or dynamic performance view or any other SQL command. Also, dictionary view NLS_SESSION_PARAMETERS shows the database character set, not the clientcharacter set!

You can run query:

Hide   Copy Code
SELECT CLIENT_CHARSET
FROM V$SESSION_CONNECT_INFO;

However, the values appear not reliable. Sometimes, it shows NULL or "unknown".

Definition of NLS_LANG

NLS_LANG can be set by Environment variable (e.g. SET NLS_LANG=AMERICAN_AMERICA.WE8MSWIN1252) or by your Registry at HKEY_LOCAL_MACHINE\Software\Oracle\KEY_{ORACLE_HOME_NAME}\NLS_LANG, resp. HKEY_LOCAL_MACHINE\Software\Wow6432Node\Oracle\KEY_{ORACLE_HOME_NAME}\NLS_LANG for 32-bit Oracle Client on a 64-bit Windows. The Environment variable takes precedence over Registry entry.

You can interrogate existing values with:

Hide   Copy Code
Windows:

reg query HKEY_LOCAL_MACHINE\Software\Oracle\KEY_{ORACLE_HOME_NAME} /f NLS_LANG
reg query HKEY_LOCAL_MACHINE\Software\Wow6432Node\Oracle\KEY_{ORACLE_HOME_NAME} /f NLS_LANG
set NLS_LANG Unix/Linux: echo $NLS_LANG

Proper Value of NLS_LANG

Usually, the values for LANGUAGE and TERRITORY are obvious and less critical in the application. The most interesting is the CHARACTER SET value. Many times, you read in forums (and sometimes even in official documentation): "The client NLS_LANG character set must be the same value as the database character set" - This is simply not true! Consider the database has two character sets, the "normal" and the national character set. On the client side, you have only one value, so actually they cannot be equal. Some character sets are available only on Client side which also vindicates my statement.

There are two requirements for the NLS_LANG character set:

  1. The NLS_LANG character set must support the characters you like to use in your application.
  2. The NLS_LANG character set must match the character set (or encoding) of your application.

Some applications/drivers load NLS_LANG definition when at launch and derive their character set from NLS_LANG value. In such case, it becomes easier and only the first requirement applies.

NLS_LANG with SQL*Plus

SQL*Plus inherits the character set from the terminal session where you started it. On Windows, you get the current character set (here called "Codepage") with chcp, the Linux/Unix equivalent is locale charmap or echo $LANG. Thus, a proper setting would be for example:

Hide   Copy Code
C:\>chcp
Active Codepage: 850. C:\>set NLS_LANG=.WE8PC850 C:\>sqlplus ...

With chcp, you can also change your codepage, e.g., chcp 1252. You can use the small batch file to change the codepage of your command line window permanently:

Hide   Shrink    Copy Code
@ECHO off

SET ROOT_KEY="HKEY_CURRENT_USER"

FOR /f "skip=2 tokens=3" %%i in _
('reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage /v OEMCP') do set OEMCP=%%i ECHO.
ECHO ...............................................
ECHO Select Codepage
ECHO ...............................................
ECHO.
ECHO 1 - CP1252
ECHO 2 - UTF-8
ECHO 3 - CP850
ECHO 4 - ISO-8859-1
ECHO 5 - ISO-8859-15
ECHO 6 - US-ASCII
ECHO.
ECHO 9 - Reset to System Default (CP%OEMCP%)
ECHO 0 - EXIT
ECHO. SET /P CP="Select a Codepage: " if %CP%==1 (
echo Set default Codepage to CP1252
reg add "%ROOT_KEY%\Software\Microsoft\Command Processor" /v Autorun /t REG_SZ /d "chcp 1252" /f
) else if %CP%==2 (
echo Set default Codepage to UTF-8
reg add "%ROOT_KEY%\Software\Microsoft\Command Processor" /v Autorun /t REG_SZ /d "chcp 65001" /f
) else if %CP%==3 (
echo Set default Codepage to CP850
reg add "%ROOT_KEY%\Software\Microsoft\Command Processor" /v Autorun /t REG_SZ /d "chcp 850" /f
) else if %CP%==4 (
echo Set default Codepage to ISO-8859-1
add "%ROOT_KEY%\Software\Microsoft\Command Processor" /v Autorun /t REG_SZ /d "chcp 28591" /f
) else if %CP%==5 (
echo Set default Codepage to ISO-8859-15
add "%ROOT_KEY%\Software\Microsoft\Command Processor" /v Autorun /t REG_SZ /d "chcp 28605" /f
) else if %CP%==5 (
echo Set default Codepage to ASCII
add "%ROOT_KEY%\Software\Microsoft\Command Processor" /v Autorun /t REG_SZ /d "chcp 20127" /f
) else if %CP%==9 (
echo Reset Codepage to System Default
reg delete "%ROOT_KEY%\Software\Microsoft\Command Processor" /v AutoRun /f
) else if %CP%==0 (
echo Bye
) else (
echo Invalid choice
pause
)

Note, the settings will apply only for the current user. If you like to set it for all users, replace line:

Hide   Copy Code
SET ROOT_KEY="HKEY_CURRENT_USER"

by:

Hide   Copy Code
SET ROOT_KEY="HKEY_LOCAL_MACHINE"

Be careful with codepage UTF-8 (chcp 65001) there is a bug, see this discussion. I do not know whether this has been fixed in more recent Windows / SQL*Plus versions.

NLS_LANG with .sql Files

When you run sql files in SQL*Plus, check the save options of your editor. Typically, you can choose values like ISO-8859-1UTF-8ANSICP1252 as encoding. Term "ANSI" denotes the default Windows code pages. On a western PC, this is CP1252.

You can interrogate default Windows code pages with:

Hide   Copy Code
C:\>reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage /v ACP

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage
ACP REG_SZ 1252 C:\>

or read "ANSI Codepage" from this table National Language Support (NLS) API Reference for any locale.

You must set character set of NLS_LANG according to the encoding of your text editor. Here is a list of available Code Pages.

NLS_LANG in Your .NET Application

  • ODP.NET Managed Driver is not NLS_LANG sensitive.
    It is only .NET locale sensitive. (See Data Provider for .NET Developer's Guide)
  • ODBC, ODP.NET and OLE DB providers from Oracle read NLS_LANG value when they are loaded and inherit the definition, resp. ensures proper character conversion for any client/database character setting.
  • ODBC, ADO.NET and OLE DB providers from Microsoft also read NLS_LANG value when they are loaded. However, they have some limitations, especially in terms of Unicode.

How to Determine the Character Set of My Application If Not Known?

First of all, you should consult the documentation of your application and used drivers.

I developed the following approach if you still have no clue about the used character set.

  • Set your NLS_LANG to NLS_LANG=.AL32UTF8
  • Connect with SQL*Plus to a database with UTF-8 support, i.e., character set AL32UTF8
    When the client character set is equal to the database character set, then no character conversion takes place and all bytes are transferred "as they are"
  • In your application, run a query with special character like this:
Hide   Copy Code
select dump('€') from dual;

DUMP('€')
-----------------
Typ=96 Len=1: 164

Then you can estimate the character set with a function written in C# like this:

Hide   Copy Code
byte[] o = new byte[] { 164 };
foreach ( var enc in Encoding.GetEncodings() ) {
var convertedString = enc.GetEncoding().GetBytes("€");
if ( convertedString.SequenceEqual(o) )
Console.WriteLine(String.Format("{0}\t{1}\t{2}", enc.CodePage, enc.Name, enc.DisplayName));
}

The function will print a list of potential character sets used by your application. Sometimes, the printout gives you obviously used character set, sometimes you have to use more other special characters. Some Codepages differ only in a single character!

What To Do If My Characters Are Still Not Properly Displayed?

  • Check carefully the documentation of your application and used drivers. Perhaps they are old and do not support Unicode yet. Make an update to the latest version of drivers.
  • Check if your font supports desired characters. You can use for example this page Font Support for Unicode Characters to verify used fonts.
  • Check the real content of your database. Run query like SELECT DUMP(THE_COLUMN, 1016) FROM ... to see the bytes in the table. Perhaps the data have been inserted by a client with wrong NLS_LANGdefinition. Don't be scared, usually you have to investigate only a few characters/bytes to get a result.

参考文献

https://docs.oracle.com/database/121/NLSPG/applocaledata.htm#GUID-9529D1B5-7366-4195-94B5-0F90F3B472E1

https://docs.oracle.com/html/B10131_02/gblsupp.htm

https://docs.oracle.com/cd/E12102_01/books/AnyInstAdm784/AnyInstAdmPreInstall18.html

https://www.unicode.org/wg2/iso10646/edition5/charts/iso10646-5th-CodeCharts.pdf

https://www.ibm.com/support/knowledgecenter/en/SS6QYM_9.2.0/com.ibm.help.install.doc/t_ConfiguringTheNLS_LANGParameterForAnOracleClient.html

转自:

https://www.codeproject.com/Tips/1068282/Setting-NLS-LANG-Value-for-Oracle

Setting NLS_LANG Value for Oracle的更多相关文章

  1. 【Oracle】详解Oracle中NLS_LANG变量的使用

    目录结构: contents structure [+] 关于NLS_LANG参数 NSL_LANG常用的值 在MS-DOS模式和Batch模式中设置NLS_LANG 注册表中NLS_LANG和系统环 ...

  2. [转帖]【Oracle】详解Oracle中NLS_LANG变量的使用

    [Oracle]详解Oracle中NLS_LANG变量的使用 https://www.cnblogs.com/HDK2016/p/6880560.html NLS_LANG=LANGUAGE_TERR ...

  3. Oracle 客户端 NLS_LANG 的设置(转)

    1. NLS_LANG 参数组成NLS_LANG参数由以下部分组成:NLS_LANG=<Language>_<Territory>.<Clients Characters ...

  4. vmware workstation9.0 RHEL5.8 oracle 10g RAC安装指南及问题总结

    一,虚拟机规划 (1)虚拟机:添加三块网卡 eth0 eth1 eth2 ,分别用于内网,心跳,外网RAC1 内网:192.168.1.10/24  心跳:192.168.2.10/24  VIP:1 ...

  5. Oracle安装前用户信息设置

    如果是重复安装,首先需要清除已经存在的软件安装记录: rm -fr /usr/local/bin/*oraenv rm -fr /usr/local/bin/dbhome rm -fr /usr/tm ...

  6. Globalization Guide for Oracle Applications Release 12

    Section 1: Overview Section 2: Installing Section 3: Configuring Section 4: Maintaining Section 5: U ...

  7. spoolight on oracle 配置

    spoolight seting 1ORACLE_HOME=D:\oracle\product\11.2.0\client_1set SQLPATH=D:\oracle\product\11.2.0\ ...

  8. Oracle 11g RAC for LINUX rhel 6.X silent install(静默安装)

    一.前期规划 1.硬件环境 CPU: Intel(R) Xeon(R) CPU E7-4820 v4 @ 2.00GHz  8*10核 内存:512GB OCR:2147*5 MB DATA1:2TB ...

  9. Linux环境下Oracle安装参数设置

    前面讲了虚拟机的设置和OracleLinux的安装,接下来我们来说下Oracle安装前的准备工作.1.系统信息查看系统信息查看首先服务器ip:192.168.8.120服务器系统:Oracle Lin ...

随机推荐

  1. 《团队作业第一周》五小福团队作业——UNO

    <团队作业第一周>团队作业--UNO 一.团队展示 队员学号 队名:五小福 (真是个红红火火恍恍惚惚的队名)> 拟作的团队项目描述 基于安卓开发的有趣味性的UNO纸牌小游戏 队员风采 ...

  2. 【AtCoder】【思维】【置换】Rabbit Exercise

    题意: 有n只兔子,i号兔子开始的时候在a[i]号位置.每一轮操作都将若干只兔子依次进行操作: 加入操作的是b[i]号兔子,就将b[i]号兔子移动到关于b[i]-1号兔子现在所在的位置对称的地方,或者 ...

  3. Mysql SQL执行错误:#1136

    情况:在插入数据时可能会遇到这种情况: 原因: 插入时的数据个数与表中的字段个数不一致 解决方法: 检查表中的字段数与代码中所插入的数据字段数是否一致 例如:以下为Salary表中结构  虽然ActI ...

  4. NEERC-2017

    A. Archery Tournament 用线段树套set维护横坐标区间内的所有圆,查询时在$O(\log n)$个set中二分查找即可. 时间复杂度$O(n\log^2n)$. #include& ...

  5. jQuery 对象 等操作

    /////////////////////下面为文件夹重命名功能区///////////////////////// $(".wpul .rename").click(functi ...

  6. hadoop解决Could not locate executable null\bin\winutils.exe in the Hadoop binaries.问题

    先看下自己的JAVA_HOME里面有没有空格目录,如果有的话,先把JAVA_HOME换个没空格的位置. 在windows系统本地运行spark的wordcount程序,会出现一个异常,但不影响现有程序 ...

  7. 小甲鱼Python第二十一讲课后习题

    测试题: 0.  递归在编程上的形式是如何表现的呢? 在编程上,递归表现为函数调用本身这么一个行为. 1.  递归必须满足哪两个基本条件? 一.        函数调用自身二.        设置了正 ...

  8. __x__(27)0907第四天__ float 浮动

     float 浮动 块元素脱离文档流,水平排列. 浮动元素 会尽量往左上(left),或者右上(right)浮动,直到遇到 块元素 或者 其他浮动元素. 可选值: none;   默认值,不脱离文档流 ...

  9. Java 初始化、final、清理

    1 为什么需要无参构造器? 第一个是继承需要 super 调用父类的构造器(父类构造器必须存在且不为 private.可以是无参/默认构造器,也可以是有参构造器),特别的如果父类不包含无参构造器的话, ...

  10. 脚本:截取euroc数据集bag文件的其中一段

    脚本:截取euroc数据集bag文件的其中一段 功能:截取euroc数据集bag中的一段供算法测试 python脚本 #!/usr/bin/env python # ----------------- ...