Differences of CHAR and VARCHAR in SQL Server Transact-SQL

Q

What Are the Differences between CHAR and VARCHAR in SQL Server Transact-SQL?

✍: FYIcenter.com

A

CHAR and VARCHAR are both used to store code page based 1-byte character strings in Transact-SQL. But they have the following main differences:

  • CHAR(n) stores character strings with a fixed length, n bytes, storage format.
  • VARCHAR(n) stores character strings with a variable length, less than n bytes, storage format.
  • If the input string of CHAR(n) has less than n bytes, it will be padded with space characters to become n bytes.
  • If the input string of VARCHAR(n) has less than n bytes, it will not be padded with any characters.
  • If the input string of CHAR(n) has more than n bytes, it will be truncated to n bytes.
  • If the input string of VARCHAR(n) has more than n bytes, it will also be truncated to n bytes.

 

Binary String Data Types in SQL Server Transact-SQL

Differences of CHAR and NCHAR in SQL Server Transact-SQL

Variables and Data Types in SQL Server Transact-SQL

⇑⇑ SQL Server Transact-SQL Tutorials

2017-04-04, 1749🔥, 0💬