When to use nvarchar/nchar, varchar/char, text/ntext

When to use nvarchar/nchar, varchar/char, text/ntext

Post by Anith Se » Wed, 22 Jan 2003 02:47:40



Quote:>> ..my app will never be international, is there any reason why I might

want to use or avoid them? <<

It is better to stick with non-UNICODE types.

Quote:>> My colleague said nvarchar and nchar datatypes are "more effecient"

than the varchar and char equivilents. Is he right? <<

AFAIK, there is nothing with proves his claim.

Quote:>> My understanding of the text datatype is that it is only preferable

when the data is larger than 8k. <<

That is pretty reasonable

Quote:>>...there any other rules I'm missing? Any to do with effeciency etc?<<

With respect to using varchar/char you can search the ng. archives here.
If you have varying sizes for your character data then VARCHAR datatype
is better and can save you space. However with VARCHAR datatypes, there
is a little more additional storage (at least 2 bytes overhead) and a
very negligible CPU time needed to keep track of the actual length of each
value. Generally, as a norm, if more than 80% of your data uses less than
80% of the maximum defined length use VARCHAR, however use common sense
and consider specific usage depending on your datamodel & data requirements.

--
- Anith
(Please respond only to newsgroups)

 
 
 

When to use nvarchar/nchar, varchar/char, text/ntext

Post by Sam » Wed, 22 Jan 2003 02:40:59


Hi

My understanding of the var- datatypes is that they are preferable when the
values in the column will vary alot. And not needed when the column data is
expected to be fairly consistent. Other than this, neither one is preferable
over the other. Have I got this right?

My understanding of the n- variables is that they are unicode datatypes. As
my app will never be international, is there any reason why I might want to
use or avoid them? My colleague said nvarchar and nchar datatypes are "more
effecient" than the varchar and char equivilents. Is he right? I don't see
why this should be the case.

My understanding of the text datatype is that it is only preferable when the
data is larger than 8k.

Are there any other rules I'm missing? Any to do with effeciency etc?

Thanks in advance

Sam

 
 
 

When to use nvarchar/nchar, varchar/char, text/ntext

Post by Aaron Bertrand [MVP » Wed, 22 Jan 2003 03:02:23


Quote:> My colleague said nvarchar and nchar datatypes are "more
> effecient" than the varchar and char equivilents. Is he right?

Only if by "more efficient" he means "only capable of holding half the data"

http://www.aspfaq.com/2354

--
Aaron Bertrand, SQL Server MVP
http://www.aspfaq.com/

Please reply in the newsgroups, but if you absolutely
must reply via e-mail, please take out the TRASH.

 
 
 

When to use nvarchar/nchar, varchar/char, text/ntext

Post by Alexei Akim » Wed, 22 Jan 2003 11:16:07


This should have been asked many times, but somehow I have not found
the answer:

if (by some chance) I have a space symbol(s), that are prefixing or
postfixing value in a varchar column, how can I detect it?

I was horrified to see, that:
1) post spaces are excluded from Length function (if prefixed, then
included), e.g.:

select Len('string1') = 7
select Len('string1                 ') = 7
but select Len('       string1') = 15 )

2) postfix space does not affect joins, like and equal statements:
e.g.
where col1 = 'string1' will happily get: 'string1 '
where col1 like 'string1' will happily get: 'string1 '

There should be easy way to distinguish between 'string1   '
and 'string1' in varchar column, but how?

3) What if the varchar value is postfixed with any CTRL symbols/esqape
sequences?

How can I see them in SQL query window and cut them out, without
third-party tools?

Thank you in advance,
Alexei

 
 
 

When to use nvarchar/nchar, varchar/char, text/ntext

Post by Aaron Bertrand [MVP » Wed, 22 Jan 2003 11:35:06


Quote:> There should be easy way to distinguish between 'string1   '
> and 'string1' in varchar column, but how?

Use a CHAR column, or convert the compared string to varchar also (so it
will drop the trailing spaces).  Once you insert 'foo     ' into a VARCHAR
column, those spaces are gone.

Note that DATALENGTH does not ignore trading spaces, regardless of
ansi_padding setting, e.g.

SET ANSI_PADDING ON
SELECT LEN('foo    ')
SELECT DATALENGTH('foo    ')

SET ANSI_PADDING OFF
SELECT LEN('foo    ')
SELECT DATALENGTH('foo    ')

returns
3
7
3
7

--
Aaron Bertrand, SQL Server MVP
http://www.aspfaq.com/

Please reply in the newsgroups, but if you absolutely
must reply via e-mail, please take out the TRASH.

 
 
 

When to use nvarchar/nchar, varchar/char, text/ntext

Post by Sam » Wed, 22 Jan 2003 17:47:11


Thanks all of you for clarifying that.

Sam


> Hi

> My understanding of the var- datatypes is that they are preferable when
the
> values in the column will vary alot. And not needed when the column data
is
> expected to be fairly consistent. Other than this, neither one is
preferable
> over the other. Have I got this right?

> My understanding of the n- variables is that they are unicode datatypes.
As
> my app will never be international, is there any reason why I might want
to
> use or avoid them? My colleague said nvarchar and nchar datatypes are
"more
> effecient" than the varchar and char equivilents. Is he right? I don't see
> why this should be the case.

> My understanding of the text datatype is that it is only preferable when
the
> data is larger than 8k.

> Are there any other rules I'm missing? Any to do with effeciency etc?

> Thanks in advance

> Sam

 
 
 

1. Char,VarChar,Text,NChar,NVarChar,Ntext

We are migrating from Access to SQL Server.
Lots of new field types: Char,VarChar,Text,NChar,NVarChar,Ntext
We can find technical descriptions of these everywhere
but could someone shed a little light on how they are used in the
everyday programming experience?

1. Which one is the equivalent to Access's MEMO field?
2. What is the best to use for simple 1-50 character fields, e.g.
LastName, FirstName, etc.

Thanks
--

Three new workshops added this week:
Blackboard, Geocities and Blogger!
www.net-language.com/workshops

2. VB5 and RDO Error

3. data types nchar/nvarchar versus char and varchar

4. Getting snapshot refreshes to work

5. CHAR/VARCHAR vs. NCHAR/NVARCHAR performance

6. handling special characters using Microsoft ODBC driver for Oracle

7. CHAR/VARCHAR -> NCHAR/NVARCHAR

8. Chair-Pak !! a backpack with a built in Chair !!

9. Differences between char, varchar, nvarchar, & ntext

10. DB-LIB dbrpcparam and nchar, nvarchar, ntext

11. SQL Server 7.0 ODBC Drivers and Unicode (nchar, nvarchar, ntext datatypes)

12. ntext, nvarchar, varchar searches

13. ntext, varchar, nvarchar searches