I am going to write a series of very short articles or tidbits on Transaction SQL (TSQL) functions. I will exploring the UNICODE() function today.
Transaction SQL (TSQL) supports the UNICODE() function that takes a NCHAR or NVARCHAR datatype as input and returns a INT datatype as output. The output value corresponds to the unicode characters location in a chart. This value can range from 0 to 65535.
The following example returns the unicode value for a space which is a value of 32.
1 2 3 4 5 |
-- A simple space declare @var_tmp1 nchar = ' '; -- Return ascii # select unicode(@var_tmp1) as unicode_val; |
1 2 3 4 5 |
output: unicode_val ----------- 32 |
One thing to always worry about when using TSQL functions is how will it react with a UNKNOWN or NULL value?
1 2 3 4 5 |
-- A unknown value declare @var_tmp2 nchar = NULL; -- Return ascii # select ascii(@var_tmp2) as unicode_val; |
1 2 3 4 5 |
output: unicode_val ----------- NULL |
Next time, I will be exploring the UPPER() function.