How Unicode() Works in SQLite

In SQLite, you can use the unicode() function to return the unicode code point for a given character.

The way it works is that it returns the unicode code point for the first character of the string that you provide.

Syntax

The syntax is quite simple:

unicode(X)

The function returns the numeric unicode code point corresponding to the first character of the string X. If the argument is not a string then the result is undefined.

Example

Here’s an example to demonstrate.

SELECT unicode('A');

Result:

65

In this case I provided a single character and so its unicode code point was returned.

Multiple Characters

As mentioned, if you provide a string that contains more than one character, only the first character’s unicode code point is returned.

SELECT unicode('Brush');

Result:

66

In this case, if I wanted to get the next character (r), I could pass substr() to unicode() and specify the position of the character.

SELECT unicode(substr('Brush', 2));

Result:

114

Here it is running through all characters.

.mode line
SELECT 
  unicode(substr('Brush', 1)),
  unicode(substr('Brush', 2)),
  unicode(substr('Brush', 3)),
  unicode(substr('Brush', 4)),
  unicode(substr('Brush', 5));

Result:

unicode(substr('Brush', 1)) = 66
unicode(substr('Brush', 2)) = 114
unicode(substr('Brush', 3)) = 117
unicode(substr('Brush', 4)) = 115
unicode(substr('Brush', 5)) = 104