What's the difference between CHAR and VARCHAR?

The most important difference is that CHAR is padded with spaces and VARCHAR is not. For example, if you have:

c1 VARCHAR(2),
c2 CHAR(2)

INSERT INTO t1 (c1,c2) VALUES ('a', 'a');

The column c1 will contain value 'a', while column c2 will contain value 'a ' with additional space. Trailing spaces are ignored when doing comparisons, so both columns would match the

WHERE c = 'a'

clause of some query. Trailing spaces are respected by LIKE operator, which is a source of confusion for beginners (example in FAQ #309).

Do you find this FAQ incorrect or incomplete? Please e-mail us what needs to be changed. To ensure quality, each change is checked by our editors (and often tested on live Firebird databases), before it enters the main FAQ database. If you desire so, the changes will be credited to your name. To learn more, visit our add content page.

All contents are copyright © 2007-2024 FirebirdFAQ.org unless otherwise stated in the text.

Links   Firebird   News   FlameRobin   Powered by FB: Home Inventory   Euchre  
Add content   About  

 Installation and setup
 Backup and restore
 Connectivity and API
 Errors and error codes