I am going to continue my series of very short articles or tidbits on Transaction SQL string functions. I will exploring the NCHAR() function today.
The NCHAR() function takes an integer value, UNICODE value from 0 to 65535 and returns a single UNICODE character string.
The following example returns the character ‘0’ given the UNICODE value of 48.
1 2 3 4 5 |
-- ASCII/UNICODE value for zero declare @var_tmp int = 48; -- Return the single character, zero select nchar(@var_tmp) as single_nchar; |
1 2 3 4 5 |
output: single_nchar ----------- 0 |
One thing to always worry about when using TSQL functions is how will it react with a UNKNOWN or NULL values?
1 2 |
-- UNKNOWN value returns NULL select nchar(NULL) as single_nchar; |
1 2 3 4 5 |
output: single_nchar ----------- NULL |
Another thing to test is how the function will work with values out of range?
1 2 3 4 5 |
-- Out of range value, lower range, returns NULL select nchar(0-1) as single_nchar; -- Out of range value, upper range, returns NULL select nchar(65535+1) as single_nchar; |
1 2 3 4 5 |
output: single_nchar ----------- NULL |
Next time, I will be exploring the PATINDEX() function.
Awsome site! I am loving it!! Will be back later to read some more. I am bookmarking your feeds also.