In Oracle Database, the NLS_CHARSET_NAME()
function returns the character set name of a given character set, based on its ID. You pass the character set ID as an argument, and the function returns its name.
Syntax
The syntax goes like this:
NLS_CHARSET_NAME(number)
Where number
is the character set ID for which you want to return the name.
Example
Here’s a basic example:
SELECT NLS_CHARSET_NAME(1)
FROM DUAL;
Result:
US7ASCII
Here it is with a different character set ID:
SELECT NLS_CHARSET_NAME(871)
FROM DUAL;
Result:
UTF8
Null Argument
If the argument is null
, the result is null
:
SET NULL 'null';
SELECT NLS_CHARSET_NAME(null)
FROM DUAL;
Result:
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 ID
Passing an invalid character set ID results in null
:
SET NULL 'null';
SELECT NLS_CHARSET_NAME(19999)
FROM DUAL;
Result:
null
However, passing the wrong data type returns an error:
SELECT NLS_CHARSET_NAME('oops')
FROM DUAL;
Result:
Error starting at line : 1 in command - SELECT NLS_CHARSET_NAME('oops') FROM DUAL Error report - ORA-01722: invalid number
Incorrect Argument Count
Calling the function without passing any arguments results in an error:
SELECT NLS_CHARSET_NAME()
FROM DUAL;
Result:
Error starting at line : 1 in command - SELECT NLS_CHARSET_NAME() 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:
But passing multiple character set IDs results in the name of the first one being returned:
SELECT NLS_CHARSET_NAME(871, 1)
FROM DUAL;
Result:
UTF8