473,544 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

varchar vs char

what's the difference of the two data types?
Nov 12 '05 #1
10 71030
Techie wrote:
what's the difference of the two data types?

var
Hope it helps,

Jan M. Nelken
Nov 12 '05 #2
Techie wrote:
what's the difference of the two data types?

The major behavioral difference I have found is the effect of trailing
blanks. With varchar, there are none unless you place them in the data
item. With char, the data is always padded out to the length of of the
data item. This can bite you in many different contexts and requires
liberal application of rtrim. IMHO char(n) should only when you certain
that now and in the future the data will always be n characters long. In
every other case varchat(n) should be used.

Nov 12 '05 #3
The char is a fixed-length character data type, the varchar is a
variable-length character data type.

Because char is a fixed-length data type, the storage size of the char
value is equal to the maximum size for this column. Because varchar is
a variable-length data type, the storage size of the varchar value is
the actual length of the data entered, not the maximum size for this
column.

You can use char when the data entries in a column are expected to be
the same size.
You can use varchar when the data entries in a column are expected to
vary considerably in size.

http://www.mssqlcity.com/FAQ/General...vs_varchar.htm
Nov 12 '05 #4
From some answers here somebody could get a wrong idea that VARCHAR
field will take different size in different rows, so VARCHAR could
save storage space.

The VARCHAR field will be same size in every row for particular
column. VARCHAR will take always two bytes more than CHAR for actual
storage, because it contains the actual length of data information
(16-bit integer).

Difference between CHAR and VARCHAR is in the speed. For same defined
length, VARCHAR could be faster if data in the CHAR column have to be
padded with space. In the VARCHAR type column, data beyond actual
length exist but are ignored.

For same defined length VARCHAR will be for two bytes bigger then
CHAR.

If you want to save space define the CHAR type column. If you need
faster processing define VARCHAR type column.

In DB2, unless you have BLOB type columns, all records are same size.
Nov 12 '05 #5
DB2SQL,

That is not correct for DB2 UDB for LUW (I can't comment on DB2 z/OS and
DB2 AS/400 internals).
DB2 for LUW will, for each row, only allocate the two bytes length, a
null indicator byte if required, and the space for the data.

Cheers
Serge
Nov 12 '05 #6
Serge Rielau wrote:
DB2SQL,

That is not correct for DB2 UDB for LUW (I can't comment on DB2 z/OS and
DB2 AS/400 internals).
DB2 for LUW will, for each row, only allocate the two bytes length, a
null indicator byte if required, and the space for the data.

Cheers
Serge


Actually, default length of the length field in VARCHAR is 4 bytes.

Jan
Nov 12 '05 #7
You first said it is not, and after that you said DB2 allocates the
space for data.

Of course, I am talking about allocated space of data. For a VARCHAR
field, the allocated space of data is same for each row regardless of
actual content of the data.

The VARCHAR field is not a BLOB. The VARCHAR field is allocated and
stored fully inside the row, so each row has same size.

There is several ways to prove that. For example, create a database
and a table with one VARCHAR field and insert million rows with an
empty VARCHAR field. Drop the database and repeat it again with some
content in the VARCHAR field. Or just take a closer look into SQLDA
with debugger.
Nov 12 '05 #8
DB2SQL wrote:
You first said it is not, and after that you said DB2 allocates the
space for data.

Of course, I am talking about allocated space of data. For a VARCHAR
field, the allocated space of data is same for each row regardless of
actual content of the data.
No, not for VARCHAR data. The string 'some text' requires 12 bytes on disc
(2 length + 1 null-indicator + 9 data bytes) whereas 'some more text' uses
17 bytes on disc (2 + 1 + 14 data bytes).
The VARCHAR field is not a BLOB. The VARCHAR field is allocated and
stored fully inside the row,
true
so each row has same size.
not true

The behavior you describe is valid for CHARACTER (CHAR) columns, though.
There is several ways to prove that. For example, create a database
and a table with one VARCHAR field and insert million rows with an
empty VARCHAR field. Drop the database and repeat it again with some
content in the VARCHAR field. Or just take a closer look into SQLDA
with debugger.


Well, only 255 rows fit on a single 4K data page. If your "some content"
shorter than some 15 bytes, you will not use the complete page for 255 rows
at all. Then, the same number of rows and 10 bytes VARCHAR data needs the
same amount of pages as a 4 bytes VARCHAR data. You should rather compare
rows with '' (empty string) and rows with REPEAT('x', 3900) and then have a
look at the tablespace details (LIST TABLESPACES SHOW DETAIL) to see how
many pages were allocated in each case. You will see a huge difference
there.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #9
Ian
DB2SQL wrote:
Of course, I am talking about allocated space of data. For a VARCHAR
field, the allocated space of data is same for each row regardless of
actual content of the data.

The VARCHAR field is not a BLOB. The VARCHAR field is allocated and
stored fully inside the row, so each row has same size.
The data is stored within the page, correct. But DB2 does not reserve
the full length of the varchar. This is why you can have rows that
overflow (i.e. an update to a VARCHAR could not fit on to the page,
so the whole row is moved to another page and a pointer is left on
the original page).

There is several ways to prove that. For example, create a database
and a table with one VARCHAR field and insert million rows with an
empty VARCHAR field. Drop the database and repeat it again with some
content in the VARCHAR field. Or just take a closer look into SQLDA
with debugger.


If you create a table with 1 column VARCHAR(254), and insert 10,000
records with a 200-character long string, vs. 10000 records with a
10-character string, you'll see roughly 20x more utilization.

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Nov 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
29478
by: dmhendricks | last post by:
Greetings, I have a question. I work on some SQL2k/ASP.NET apps at work. My predacessor, who created the databases/tables seemed to have liked to use 'char' for all text fields. Is there a reason why he would have done this over using varchar? It's a minor annoyance to always have to RTRIM data and it makes directly making changes to...
5
10421
by: twkelsey | last post by:
Hi, My company has a scenario where we would like to change the data type of an existing primary key from an integer to a char, but we are concerned about the performance implications of doing so. The script for the two tables that we need to modify is listed below. Table FR_Sessions contains a column named TransmissionID which is...
4
5594
by: David Cho | last post by:
When I run the code below, SELECT LEN(@mytext) prints 300 as expected. Yet SELECT @mytext only prints the first 256 characters. Isn't varchar supposed to have up to 8000? Is there something from the discrete math class I am forgetting here? Sorry, that was over 10 years ago. DECLARE @mytext varchar(500) SELECT @mytext = '1111111111'
11
60185
by: mailar | last post by:
Hi, I am using DB2 version 8.2 on Windows XP platform. I have a table 'EMP' with one field named 'NAME' of type CHAR , size 80. Also , I have a record with NAME='john' Now, when I try to find the length of the this name field DB2 returns 80 . Can someone tell me how do I find the actual length of this NAME
22
11558
by: jdokos | last post by:
Hello, I have a question about VARCHAR fields. Our application groups here are starting to use VARCHARs much more frequently. Even VARCHAR (2) to (9) length fields. They say this is because some of the application programs, specifically Java Beans cannot handle the spaces after the value in CHAR fields. Is anyone else seeing this...
1
6609
by: Troels Arvin | last post by:
Hello, In the DBMS I know best, PostgreSQL, there is no real performance difference between CHAR and VARCHAR. And since CHAR pads with spaces, I generally dislike CHAR (with CHAR, it's not possible to see if the input value had trailing spaces or not, for example). How is it in DB2? More specifically: If a column has type CHAR(20) and...
7
20431
by: D. | last post by:
Hi, I'm planning the structure of a SqlServer 2005 database for a new application. The requirement is that primary keys must be "natural"; i.e. in the table Customers the primary key will be a max. 10 characters string (but the string may be filled i.e. with only 5 charachters). Should I define these primary keys as char or varchar? I'm...
4
8852
by: Nick Chan | last post by:
all these while i've only used varchar for any string i heard from my ex-boss that char helps speed up searches. is that true? so there are these: 1) char with index 2) char without index 3) char with clustered index
0
27227
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications are constrained by the technology available at the time of their development and hence aren’t optimum as per current scenario. One of such cases is the...
0
7365
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7607
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7772
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7376
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7709
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5297
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4918
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
988
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
661
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.