One of the functions included in T-SQL is the UNICODE()
function. You can use this function with SQL Server (and Azure) to return the Unicode value of a given character.
This function works similar to the ASCII()
function, except that it returns the Unicode value.
Syntax
Here’s the official syntax:
UNICODE ( 'ncharacter_expression' )
Where ncharacter_expression is an nchar or nvarchar expression for which you want the Unicode value for.
Example
So as an example, if we wanted to find out the Unicode value for the Euro sign, we could do this:
SELECT UNICODE('€');
Result:
8364
This result shows us that the Unicode value for the Euro sign is 8364.
Unicode vs ASCII
The UNICODE()
function works similar to the ASCII()
function, but with the exception that it returns the Unicode value as opposed to the ASCII value.
Note that, for the first 128 characters, the ASCII and Unicode values are the same, and therefore, these two functions will produce the same results for the first 128 characters. However, the UNICODE()
function will work with a much larger range of characters, due to the 128 character limitation of the ASCII standard.
Here’s an example of the ASCII()
and UNICODE()
functions side by side:
SELECT UNICODE('A') AS Unicode, ASCII('A') AS ASCII;
Result:
Unicode ASCII ------- ----- 65 65
So we can see that they returned the same result in this case. This is due to the fact that the letter A
falls within the range of ASCII codes (and therefore is also included within the Unicode range).