How do you use the NCHAR function to return the Unicode character for a given code point?
Posted by KarenKg
Last Updated: July 09, 2024
The NCHAR function in SQL is used to return the Unicode character corresponding to a specified code point. It is widely supported in SQL databases like SQL Server, and it can be particularly useful when you need to work with special characters or symbols that are represented by Unicode. The syntax for the NCHAR function is as follows:
NCHAR(code_point)
Parameters:
- code_point: An integer representing the Unicode code point of the character you want to retrieve. The code point should be in the range of 0 to 1114111, which corresponds to the valid characters in the Unicode standard.
Example Usage:
Here are a few examples of how to use the NCHAR function: 1. Getting a Basic Character: To get the Unicode character for the code point 65, which corresponds to the character 'A':
SELECT NCHAR(65) AS Character;
This will return:
Character
   ----------
   A
2. Getting a Special Character: To get the Unicode character for the code point 9731, which corresponds to a snowman:
SELECT NCHAR(9731) AS Snowman;
This will return:
Snowman
   ----------
   ?
3. Using in a Larger Query: You can also utilize NCHAR in more complex queries. For example, if you want to concatenate characters to form a string:
SELECT NCHAR(72) + NCHAR(101) + NCHAR(108) + NCHAR(108) + NCHAR(111) AS Greeting;
This would return:
Greeting
   ----------
   Hello
Points to Note:
- Ensure that the code point you provide is a valid integer within the allowed range. - Different SQL database systems may have slight variations in how they implement functions, so consult the specific documentation if you're using a database other than SQL Server. However, NCHAR is generally standard in systems that support Unicode.