In standard SQL, CHAR (also written as CHARACTER) is a fixed-length string data type. When you declare a column as CHAR(N), every stored value is padded on the right with spaces until it reaches exactly N characters. This predictable storage size can speed up certain index and memory operations compared with variable-length VARCHAR fields, especially for short, uniform codes such as country or status indicators.Some dialects (MySQL, SQL Server, MariaDB) also expose a CHAR( ) scalar function that returns the character corresponding to one or more numeric ASCII/Unicode code points. While the data-type and the function share a name, they serve different purposes.Key behaviors:- Length must be a positive integer. Omitting it defaults to 1 in most systems (CHAR equals CHAR(1)).- On insert or update, shorter strings are space-padded; longer strings raise an error or are truncated, depending on SQL mode.- Trailing spaces are considered part of the value but many comparisons ignore them, which can cause subtle bugs.- Collation and character-set rules apply exactly as for VARCHAR.- Converting CHAR to VARCHAR generally removes trailing spaces; converting the other way adds them.Caveats:- Space padding increases storage when N is large or highly variable.- Equality tests against literals must include padding in some engines unless ANSI padding rules are active.- In Oracle, fixed-length semantics apply only to CHAR, while NCHAR is used for Unicode fixed-length data.- The CHAR( ) function is not available in PostgreSQL (use CHR) or SQLite (use CHAR()). Always verify the correct function name.
length
(integer) - Required for the data type (default 1). Defines the exact number of characters to store.numeric_code
(integer) - One or more ASCII/Unicode code points to convert to characters when using the function form.VARCHAR, CHAR VARYING, NCHAR, CHR(), COLLATE, CHARACTER SET
ANSI SQL-86
Pick the smallest length that still fits every possible value. Common examples: country codes (2), gender (1), and checksum letters (1).
SQL’s ANSI padding rules treat fixed-length strings of the same length as equal even if the right side is padded. Some engines trim spaces during comparison, making 'A' equal to 'A '.
Yes, but the engine will pad existing data. Always verify storage impact and update any code that assumes trimmed values.
CHAR stores whatever the column’s character set supports. Declare CHAR in a Unicode-enabled database or use NCHAR to guarantee UTF-16 in SQL Server and Oracle.