NCHAR
NCHAR is a SQL function that creates a character string type. It's unique because it can store Unicode characters, allowing for a diverse set of characters from various languages to be held within a database. The NCHAR string type supports a fixed length which must be specified during declaration.
NCHAR( integer_expression )
- integer_expression: This parameter specifies the length for the NCHAR function; it defines the number of characters to be returned. The value must be between 1 and 4000. Although the input can be a constant, variable, or returned from a scalar function, it must always be an integer. The function will return a Unicode string of fixed length, with the length specified being the length of the returned string.
Example
Output
Explanation
The NCHAR
function in SQL Server converts an integer ASCII code to a character. Here, it converts ASCII code 65, which corresponds to the letter ‘A’, into its corresponding character representation.
NCHAR(n)
- n: This is the length of the string that will be returned by the NCHAR function. It determines the number of characters or the size of the characters that the function will accommodate. If n is not specified, the function will return a single character. The maximum size allowed is 2000 bytes.
Example
Output
Explanation
The NCHAR function converts numbers into Unicode characters. In this case, it converts the number 2673 to the Unicode symbol for the planet Saturn (♄).