Going Stellar

Home/Google Sheets reference

Google Sheets function · Text

CODE in Google Sheets: syntax and examples

CODE turns the first character of a text into its Unicode number. It is the reverse of CHAR. Use it to compare letters, sort by character or find hidden characters in imported data.

Syntax

CODE(string)

Arguments

ArgumentTypeRequiredWhat it does
stringstringyesThe text. Only its first character is used.

Examples

The examples use this table, starting in A1:

Text
A
apple
€
7
=CODE("A")

Result: 65

Code of an uppercase letter.

Run in a Google Sheet (English US locale) on .

=CODE("a")

Result: 97

The lowercase letter has a different code.

Run in a Google Sheet (English US locale) on .

=CODE(A3)

Result: 97

Only the first character counts, so the rest of the word is ignored.

Run in a Google Sheet (English US locale) on .

=CODE(A4)

Result: 8364

A character outside basic ASCII returns its Unicode value.

Run in a Google Sheet (English US locale) on .

=CHAR(CODE(A2)+1)

Result: B

Shift a letter by one position: CODE gives the number, CHAR turns it back into a letter.

Run in a Google Sheet (English US locale) on .

Common errors

  • Only the first character is read. CODE("apple") returns 97, the code of "a".
  • An empty string returns #VALUE!. Wrap it in IFERROR if the source can be blank.
  • Case matters: "A" is 65 and "a" is 97.
  • A number is read as text. CODE(5) returns 53, the code of the digit "5", not 5.

Compared with Excel

Same syntax in Excel.

Sources

  1. CODE (Docs Editors Help)

Markdown version · All functions