Home All Groups Group Topic Archive Search About

Even after year of coding I ask...

Author
9 Sep 2006 2:27 AM
Chris
Hi,
When really should I use

Char(n)
Varchar(n)
NVarchar(n)

I have seen many definitions but I need an expert to confirm. I know
NVarchar in non-unicode, however, what is this unicode and non-unicode. Spare
my ignorance. Oh... and Joe Celko if you happen to spot this, try not to bang
me around, seriously.

Thanks

Author
9 Sep 2006 3:01 AM
Kalen Delaney
There's also NCHAR, don't forget. :-)

Actually, you have it backwards. Nvarchar and nchar are Unicode, with the N
indicating 'National'.
Unicode characters take 2 bytes each, so you need twice as much space for
storage, but they can hold way more types of characters in a wide variety of
languages.  You can read about Unicode data in the Books Online, there is a
good discussion of when it is useful.

The bigger decision is fixed vs variable length. Variable length can save
you space if your data comes in a wide variety of lengths, but it can cost
you time. With variable length data, SQL Server must figure out the starting
and ending position of the column in every row, because the offsets in the
row are not constant. There can also be more overhead when updating data,
because you can update a row, and increase the size, so that there is no
longer room for the row on the page where you found it.

You really should run some tests with your data and your operations to see
what kind of difference you get with the two datatypes.
A rule of thumb I heard once, but have never proved to myself, is "If 50% or
more of your data uses 80% or more of the maximum length, use fixed length.
Otherwise, use variable length.

Another issue, when using SQL Server 2005, is that variable length data can
actually allow the row size to exceed the maximum page size. The data is
then stored 'off the row' in its own special pages. The maximum page size is
8092 bytes, but you can define two varchar(5000) columns, for example. You
cannot define two char(5000) columns, however. Then there is varchar(max),
but that's another whole topic.

--
HTH
Kalen Delaney, SQL Server MVP


Show quote
"Chris" <Ch***@discussions.microsoft.com> wrote in message
news:43B6CA51-2A8A-4582-B349-03483C898F71@microsoft.com...
> Hi,
> When really should I use
>
> Char(n)
> Varchar(n)
> NVarchar(n)
>
> I have seen many definitions but I need an expert to confirm. I know
> NVarchar in non-unicode, however, what is this unicode and non-unicode.
> Spare
> my ignorance. Oh... and Joe Celko if you happen to spot this, try not to
> bang
> me around, seriously.
>
> Thanks
Author
9 Sep 2006 2:11 PM
--CELKO--
Unicode is a 16 bit per character international standard that is meant
to display just about every language on earth.  Because writing systems
are different, there are also codes for placement of the characters --
look at accent marks in Viet Namee, the four cases in Arabic, etc.

www.unicode.org should tell you more than you want to know.
Author
11 Sep 2006 3:08 PM
Chris
Thank  --CELKO--,
I admire your work.

Show quote
"--CELKO--" wrote:

> Unicode is a 16 bit per character international standard that is meant
> to display just about every language on earth.  Because writing systems
> are different, there are also codes for placement of the characters --
> look at accent marks in Viet Namee, the four cases in Arabic, etc.
>
> www.unicode.org should tell you more than you want to know.
>
>
Author
12 Sep 2006 4:18 AM
--CELKO--
>>  I admire your work. <<

Thank you.  Now go and make some software better that it was before --
the best lafterty  :)!
Author
10 Sep 2006 7:12 AM
Tony Rogerson
If you only ever need ASCII for a column then don't use Nvarchar etc....

On SQL collation there is a huge difference in performance for string
searching (LIKE); if you windows collation then that difference in
performance goes.

I'll blog about the performance differences when I get chance.

--
Tony Rogerson
SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson - technical commentary from a SQL
Server Consultant
http://sqlserverfaq.com - free video tutorials


Show quote
"Chris" <Ch***@discussions.microsoft.com> wrote in message
news:43B6CA51-2A8A-4582-B349-03483C898F71@microsoft.com...
> Hi,
> When really should I use
>
> Char(n)
> Varchar(n)
> NVarchar(n)
>
> I have seen many definitions but I need an expert to confirm. I know
> NVarchar in non-unicode, however, what is this unicode and non-unicode.
> Spare
> my ignorance. Oh... and Joe Celko if you happen to spot this, try not to
> bang
> me around, seriously.
>
> Thanks
Author
11 Sep 2006 3:09 PM
Chris
Looking forward to view the blogE.

Thanks

Show quote
"Tony Rogerson" wrote:

> If you only ever need ASCII for a column then don't use Nvarchar etc....
>
> On SQL collation there is a huge difference in performance for string
> searching (LIKE); if you windows collation then that difference in
> performance goes.
>
> I'll blog about the performance differences when I get chance.
>
> --
> Tony Rogerson
> SQL Server MVP
> http://sqlblogcasts.com/blogs/tonyrogerson - technical commentary from a SQL
> Server Consultant
> http://sqlserverfaq.com - free video tutorials
>
>
> "Chris" <Ch***@discussions.microsoft.com> wrote in message
> news:43B6CA51-2A8A-4582-B349-03483C898F71@microsoft.com...
> > Hi,
> > When really should I use
> >
> > Char(n)
> > Varchar(n)
> > NVarchar(n)
> >
> > I have seen many definitions but I need an expert to confirm. I know
> > NVarchar in non-unicode, however, what is this unicode and non-unicode.
> > Spare
> > my ignorance. Oh... and Joe Celko if you happen to spot this, try not to
> > bang
> > me around, seriously.
> >
> > Thanks
>
>
>

AddThis Social Bookmark Button