NLS_CHARSET_ID() Function in Oracle

In Oracle, the NLS_CHARSET_ID() function returns the character set ID number of a given character set. You pass the character set name as an argument, and it returns its ID.

Syntax

The syntax goes like this:

NLS_CHARSET_ID(string)

Where string is the character set name for which you want to return the ID.

Example

Here’s a basic example:

SELECT NLS_CHARSET_ID('AL16UTF16')
FROM DUAL;

Result:

2000

Here it is with a different character set:

SELECT NLS_CHARSET_ID('UTF8')
FROM DUAL;

Result:

871

Null Argument

If the argument is null, the result is null:

SET NULL 'null';
SELECT NLS_CHARSET_ID(null)
FROM DUAL;

Result:

   NLS_CHARSET_ID(NULL) 
_______________________ 
                   null

By default, SQLcl and SQL*Plus return a blank space whenever null occurs as a result of a SQL SELECT statement.

However, you can use SET NULL to specify a different string to be returned. Here I specified that the string null should be returned.

Invalid Character Set Names

Passing an invalid character set name results in null:

SET NULL 'null';
SELECT NLS_CHARSET_ID('oops')
FROM DUAL;

Result:

   NLS_CHARSET_ID('OOPS') 
_________________________ 
                     null 

Incorrect Argument Count

Calling the function without passing any arguments results in an error:

SELECT NLS_CHARSET_ID()
FROM DUAL;

Result:

Error starting at line : 1 in command -
SELECT NLS_CHARSET_ID()
FROM DUAL
Error at Command Line : 1 Column : 8
Error report -
SQL Error: ORA-00938: not enough arguments for function
00938. 00000 -  "not enough arguments for function"
*Cause:    
*Action:

However, passing multiple character set names results in the ID of the first one being returned:

SELECT NLS_CHARSET_ID('UTF8', 'AL16UTF16')
FROM DUAL;

Result:

871