SQL Keywords

SQL VARCHARACTER

What is SQL VARCHARACTER?

Variable-length character string data type that stores up to a user-defined number of characters.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL VARCHARACTER:

SQL VARCHARACTER Full Explanation

VARCHARACTER (commonly written VARCHAR or CHARACTER VARYING) is the ANSI-standard variable-length character string type. Unlike CHAR, it stores only the actual characters supplied and does not right-pad with spaces. A maximum length must be supplied in parentheses; this length is measured in characters, not bytes, although some dialects enforce byte limits internally. When a value longer than the declared limit is inserted, the database either rejects the row, truncates the value, or raises a warning depending on the dialect and SQL mode. Indexing, collation, and case-sensitivity follow the database’s rules for string types. Storage overhead usually includes one or two bytes per row to record the string’s actual length. In most systems, VARCHARACTER and VARCHAR are synonyms; Oracle uses VARCHAR2, and SQL Server distinguishes between VARCHAR (single-byte) and NVARCHAR (Unicode).

SQL VARCHARACTER Syntax

column_name VARCHARACTER(n)
-- ANSI alias
column_name VARCHAR(n)
-- Equivalent in most systems
column_name CHARACTER VARYING(n)

SQL VARCHARACTER Parameters

  • n (integer) - Maximum number of characters that can be stored (required, 1 to dialect-specific upper limit)
  • encoding (implicit) - Character set follows the table or column collation/charset settings

Example Queries Using SQL VARCHARACTER

-- Create a table with VARCHARACTER
CREATE TABLE customers (
  id           INT PRIMARY KEY,
  email        VARCHARACTER(255) NOT NULL,
  first_name   VARCHARACTER(50),
  last_name    VARCHARACTER(50)
);

-- Insert within the limit
INSERT INTO customers VALUES (1,'ada@example.com','Ada','Lovelace');

-- Attempt to exceed limit (will fail or truncate)
INSERT INTO customers (id,email) VALUES (2, REPEAT('a',260));

-- Alter column length
ALTER TABLE customers ALTER COLUMN email SET DATA TYPE VARCHARACTER(320);

-- Query length of stored value
SELECT id, LENGTH(email) AS chars FROM customers;

Expected Output Using SQL VARCHARACTER

  • Successful inserts store only the characters provided and record their actual length
  • The over-length insert raises an error or truncates according to the database’s configuration
  • The LENGTH query returns the true character count, not the declared maximum

Use Cases with SQL VARCHARACTER

  • Store user names, email addresses, URLs, or any text with unpredictable length but a known upper bound.
  • Reduce storage compared to CHAR when values vary widely in size.
  • Define columns that will be part of indexes where very long TEXT types are disallowed.

Common Mistakes with SQL VARCHARACTER

  • Forgetting to specify a length, which is invalid in most dialects.
  • Confusing character count with byte count, especially with multi-byte UTF-8 characters.
  • Assuming trailing spaces are stored (they are not).
  • Setting the length too low and encountering truncation or errors.
  • Using VARCHARACTER when an unlimited TEXT/CLOB type is more appropriate.

Related Topics

First Introduced In

SQL-92

Frequently Asked Questions

What is the maximum length I can set for VARCHARACTER?

Maximum length is dialect-specific: 65,535 in MySQL, 1 GB in PostgreSQL, 8,000 for SQL Server VARCHAR (2 GB for VARCHAR(MAX)).

Does VARCHARACTER waste space for short strings?

No. It stores only the actual characters plus a small length prefix, unlike CHAR which always uses the fixed size.

How does VARCHARACTER handle Unicode characters?

Most databases store Unicode directly when the column or database uses a UTF charset. Length is still counted in characters.

Can I change a VARCHARACTER column to a longer size later?

Yes. Use ALTER TABLE ... ALTER COLUMN ... SET DATA TYPE VARCHARACTER(new_size). Shortening the size requires checking that no existing value exceeds the new limit.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!