SQL Keywords

SQL VARCHAR

What is SQL VARCHAR?

VARCHAR stores variable-length character strings up to a defined maximum.
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 VARCHAR: PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite, Snowflake, Redshift

SQL VARCHAR Full Explanation

VARCHAR (or CHARACTER VARYING) is a standard SQL data type used to store character strings whose length can vary per row, up to a user-defined limit. Unlike CHAR, which pads unused space with blanks, VARCHAR uses only as much space as needed plus a small length header, making it more space-efficient for columns where string lengths differ significantly. Each RDBMS enforces its own maximum length (often 65,535 in MySQL, 2 GB in PostgreSQL and SQL Server) and may differ in whether trailing spaces are preserved or trimmed. When defining a VARCHAR column you must specify a positive integer length. If a value longer than this length is inserted, the database either truncates the data or throws an error, depending on the dialect and SQL mode. VARCHAR participates in collation and character-set rules just like other character types. Because storage is length-prefixed, random updates that increase row size can cause page splits or row overflow in some engines, a performance consideration on very large tables.

SQL VARCHAR Syntax

CREATE TABLE table_name (
    column_name VARCHAR(n)
);
-- or in SELECT/CAST
SELECT CAST('abc' AS VARCHAR(10));

SQL VARCHAR Parameters

  • n (integer) - Required maximum number of characters that each stored string can hold.

Example Queries Using SQL VARCHAR

-- 1. Create a table with VARCHAR columns
CREATE TABLE employees (
    id INT PRIMARY KEY,
    first_name VARCHAR(30),
    last_name VARCHAR(30),
    email VARCHAR(255)
);

-- 2. Insert data within the limit
INSERT INTO employees (id, first_name, last_name, email)
VALUES (1, 'Ada', 'Lovelace', 'ada.lovelace@example.com');

-- 3. Attempt to insert data that exceeds the limit (will error or truncate)
INSERT INTO employees (id, first_name, last_name, email)
VALUES (2, 'A_very_long_first_name_exceeding_thirty_chars', 'Byron', 'long@example.com');

-- 4. Change data type length
ALTER TABLE employees ALTER COLUMN email TYPE VARCHAR(320);

-- 5. Cast a literal
SELECT CAST('hello' AS VARCHAR(10));

Expected Output Using SQL VARCHAR

  • Table is created and ready for inserts.
  • One row inserted successfully.
  • Database returns an error or truncates the first_name value depending on dialect settings.
  • Column email length increased, existing data preserved.
  • Query returns a single column containing the string 'hello'.

Use Cases with SQL VARCHAR

  • Store names, emails, URLs, or any text where length varies
  • Minimize storage compared to fixed-width CHAR
  • Enforce upper length limits for validation
  • Combine with indexes for search on variable text

Common Mistakes with SQL VARCHAR

  • Omitting length: Some dialects require VARCHAR(n); leaving out n causes an error.
  • Choosing an unnecessarily small n and hitting truncation errors.
  • Assuming all trailing spaces are preserved; some systems trim them.
  • Believing VARCHAR is always faster than CHAR; for very small, fixed-length fields CHAR can be faster.

Related Topics

CHAR, TEXT, NVARCHAR, VARCHAR2, CHARACTER VARYING

First Introduced In

SQL-92

Frequently Asked Questions

What is the difference between VARCHAR and CHAR?

CHAR stores fixed-length strings and pads unused space with blanks, while VARCHAR stores variable-length strings without padding, saving storage when values differ in length.

Does VARCHAR length affect performance?

For small strings, CHAR can be marginally faster due to fixed width. For varied lengths, VARCHAR reduces I/O and usually offers better overall performance.

Can I change the length of a VARCHAR column later?

Yes. Use ALTER TABLE to modify the column length. Increasing length is safe; decreasing may fail if data exceeds the new limit.

How does VARCHAR handle Unicode?

Most modern databases allow specifying a Unicode character set (e.g., UTF8) on the column or database level. VARCHAR then stores multibyte characters transparently, with the length limit still defined in characters (not bytes).

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!