Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

data type lengths

 
Goto page 1, 2
   Database Forums (Home) -> Programming RSS
Next:  UNION in SQL Server 2005  
Author Message
Smokey Grindel

External


Since: Dec 11, 2006
Posts: 39



(Msg. 1) Posted: Tue Feb 12, 2008 8:59 am
Post subject: data type lengths
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Ok so nvarchar and varchar are unicode and ansi... well all the books I read
say that you specify the length in number of bytes... so nvarchar takes 2
bytes per char... varchar takes 1.... well they claim these are you you
represent a 10 char in each

varchar(10)
nvarchar(20)

now here is where I am confused... ok the varchar(10) takes 10 chars with no
problems... but I can also put 20 chars into the nvarchar... why? all the
books claim only 10 will fit... now if i put 21 I get a truncation error
from sql server... what am I missing that the books are not telling me?

 >> Stay informed about: data type lengths 
Back to top
Login to vote
Alejandro Mesa

External


Since: Mar 13, 2006
Posts: 76



(Msg. 2) Posted: Tue Feb 12, 2008 8:59 am
Post subject: RE: data type lengths [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Smokey Grindel,

varchar(n) / nvarchar(n)

n - number of characters

nvarchar uses 2 bytes percharacters, so:

varchar(10) - could use a maximum of 10 bytes
nvarchar(10) - could use a maximum of 20 bytes


AMB



"Smokey Grindel" wrote:

> Ok so nvarchar and varchar are unicode and ansi... well all the books I read
> say that you specify the length in number of bytes... so nvarchar takes 2
> bytes per char... varchar takes 1.... well they claim these are you you
> represent a 10 char in each
>
> varchar(10)
> nvarchar(20)
>
> now here is where I am confused... ok the varchar(10) takes 10 chars with no
> problems... but I can also put 20 chars into the nvarchar... why? all the
> books claim only 10 will fit... now if i put 21 I get a truncation error
> from sql server... what am I missing that the books are not telling me?
>
>
>

 >> Stay informed about: data type lengths 
Back to top
Login to vote
sparker

External


Since: Jun 17, 2007
Posts: 4



(Msg. 3) Posted: Tue Feb 12, 2008 8:59 am
Post subject: RE: data type lengths [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I think you have accidently confused the number of characters the column will
hold versus the number of bytes required to store the characters in terms of
disk space.

~ SPARKER ~


"Smokey Grindel" wrote:

> Ok so nvarchar and varchar are unicode and ansi... well all the books I read
> say that you specify the length in number of bytes... so nvarchar takes 2
> bytes per char... varchar takes 1.... well they claim these are you you
> represent a 10 char in each
>
> varchar(10)
> nvarchar(20)
>
> now here is where I am confused... ok the varchar(10) takes 10 chars with no
> problems... but I can also put 20 chars into the nvarchar... why? all the
> books claim only 10 will fit... now if i put 21 I get a truncation error
> from sql server... what am I missing that the books are not telling me?
>
>
>
 >> Stay informed about: data type lengths 
Back to top
Login to vote
Steven Wilmot

External


Since: Feb 11, 2008
Posts: 3



(Msg. 4) Posted: Tue Feb 12, 2008 11:01 am
Post subject: Re: data type lengths [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Smokey Grindel" wrote in message


> now here is where I am confused... ok the varchar(10) takes 10 chars with
> no problems... but I can also put 20 chars into the nvarchar... why? all
> the books claim only 10 will fit... now if i put 21 I get a truncation
> error from sql server... what am I missing that the books are not telling
> me?
>

I would find it surprising for any book to tell you that "nvarchar(x)" will
only STORE x/2 characters

Is far as I am aware, it has always been the case that
varchar(x) will hold a maximum of x characters (each occupying one 8-bit
byte)
and
nvarchar(x) will hold a maxium of x characters (each occupying two 8-bit
bytes)
therefore having a storage length of "2x"

See: http://msdn2.microsoft.com/en-us/library/ms186939.aspx

Also:
http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
(The SQL-92 standard)#

---

Are you able to able to provide a reference to any book / web-page that
indicates the opposite ?

Steven
 >> Stay informed about: data type lengths 
Back to top
Login to vote
Smokey Grindel

External


Since: Dec 11, 2006
Posts: 39



(Msg. 5) Posted: Tue Feb 12, 2008 11:01 am
Post subject: Re: data type lengths [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Well be suprised then... "SQL SERVER 2005: Implementation and Maintenance
Trainking Kit" book by Solid Quality Learning says and I quote "For example,
a char(10) can store a maximum 10 characters because each character requires
one byte of storeage, whereas an nchar(10) can store a maximum of five
characters because each Unicode character requires two bytes of storage."
every book ive read says something similar to that...

"Steven Wilmot" wrote in message

>
> "Smokey Grindel" wrote in message
>
>
>> now here is where I am confused... ok the varchar(10) takes 10 chars with
>> no problems... but I can also put 20 chars into the nvarchar... why? all
>> the books claim only 10 will fit... now if i put 21 I get a truncation
>> error from sql server... what am I missing that the books are not telling
>> me?
>>
>
> I would find it surprising for any book to tell you that "nvarchar(x)"
> will only STORE x/2 characters
>
> Is far as I am aware, it has always been the case that
> varchar(x) will hold a maximum of x characters (each occupying one
> 8-bit byte)
> and
> nvarchar(x) will hold a maxium of x characters (each occupying two
> 8-bit bytes)
> therefore having a storage length of "2x"
>
> See: http://msdn2.microsoft.com/en-us/library/ms186939.aspx
>
> Also:
> http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
> (The SQL-92 standard)#
>
> ---
>
> Are you able to able to provide a reference to any book / web-page that
> indicates the opposite ?
>
> Steven
>
 >> Stay informed about: data type lengths 
Back to top
Login to vote
Alex Kuznetsov

External


Since: Oct 12, 2007
Posts: 237



(Msg. 6) Posted: Tue Feb 12, 2008 11:01 am
Post subject: Re: data type lengths [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Feb 12, 10:31 am, "Smokey Grindel" wrote:
> yep.. wish they'd have an updated download site or something we could get
> revisions for... the books say where to submit corrections to in them dont
> they? kinda remember MS press books having somewhere to do that..
>
> "Aaron Bertrand [SQL Server MVP]" wrote in messagenews:ulxb1JZbIHA.3484@TK2MSFTNGP06.phx.gbl...
>
> > Sounds backward to me... it is much easier (never mind financially
> > feasible) for them to update the PDF than a printed book. Especially if
> > you already own the book... it's not like a recall on a car. Smile
>
> > Of course just like a printed book, they can't update a PDF you downloaded
> > a long time ago (or that was distributed on the CD with the book).
>
> > "Smokey Grindel" wrote in message
> >
> >> I'll look those up again, I've been reading these one at a time so its
> >> been a few weeks since ive been through the others... the one I am on now
> >> is is ISBN-13: 978-0-7356-2271-5 Page 113 last paragraph...
>
> >> The wierd thing I think the eBook versions that come with the books are
> >> out of date... I just went looking in the print book for that quote and
> >> its not there on that page... but the ebook has it there... maybe they
> >> just didnt update the PDF ebooks?
>
> >> The other error was in T-SQL Programming INSIDE Microsoft SQL SERVER 2005
> >> have to find that page though i dont have the ebook on hand for it but i
> >> know it was there in the ebook cant seem to find it in print though...
> >> maybe the ebooks are just messed up

see for yourself, and trust your own eyes more than any book:

create table #t(c varchar(10), n nvarchar(10))
insert #t(c,n) values('1234567890','1234567890')
--------------
success

insert #t(c,n) values('1234567890a','1234567890')
-----------------------
Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.
The statement has been terminated.

insert #t(c,n) values('1234567890','1234567890a')
--------------
Msg 8152, Level 16, State 4, Line 1
String or binary data would be truncated.
The statement has been terminated.

insert #t(c,n) values('1234567890','ÁÂ×ÇÄÅÖÚÉË')
--------------
success

insert #t(c,n) values('1234567890','ÁÂ×ÇÄÅÖÚÉËÌ')
---------------------------------------
Msg 8152, Level 16, State 4, Line 1
String or binary data would be truncated.
The statement has been terminated.
 >> Stay informed about: data type lengths 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jun 15, 2005
Posts: 632



(Msg. 7) Posted: Tue Feb 12, 2008 11:01 am
Post subject: Re: data type lengths [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

The book is wrong. The n dictates number of characters to allow, not the
number of bytes to restrict storage.


> Well be suprised then... "SQL SERVER 2005: Implementation and Maintenance
> Trainking Kit" book by Solid Quality Learning says and I quote "For
> example, a char(10) can store a maximum 10 characters because each
> character requires one byte of storeage, whereas an nchar(10) can store a
> maximum of five characters because each Unicode character requires two
> bytes of storage." every book ive read says something similar to that...
 >> Stay informed about: data type lengths 
Back to top
Login to vote
Smokey Grindel

External


Since: Dec 11, 2006
Posts: 39



(Msg. 8) Posted: Tue Feb 12, 2008 11:01 am
Post subject: Re: data type lengths [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Yeah figured its wrong when I read that something just didn't feel right
because I new I've entered that number as number of chars in the past for
nvarchar... but I just found 3 other books on my shelf by MS Press that say
the same thing.... is anyone proofing these books?....

"Aaron Bertrand [SQL Server MVP]" wrote in message

> The book is wrong. The n dictates number of characters to allow, not the
> number of bytes to restrict storage.
>
>
>> Well be suprised then... "SQL SERVER 2005: Implementation and
>> Maintenance Trainking Kit" book by Solid Quality Learning says and I
>> quote "For example, a char(10) can store a maximum 10 characters because
>> each character requires one byte of storeage, whereas an nchar(10) can
>> store a maximum of five characters because each Unicode character
>> requires two bytes of storage." every book ive read says something
>> similar to that...
>
>
 >> Stay informed about: data type lengths 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jun 15, 2005
Posts: 632



(Msg. 9) Posted: Tue Feb 12, 2008 11:01 am
Post subject: Re: data type lengths [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> Yeah figured its wrong when I read that something just didn't feel right
> because I new I've entered that number as number of chars in the past for
> nvarchar... but I just found 3 other books on my shelf by MS Press that
> say the same thing.... is anyone proofing these books?....

Book titles, page numbers? I have plenty of MS Press books on my shelf. I
don't have the book you originally mentioned though.
 >> Stay informed about: data type lengths 
Back to top
Login to vote
Smokey Grindel

External


Since: Dec 11, 2006
Posts: 39



(Msg. 10) Posted: Tue Feb 12, 2008 11:01 am
Post subject: Re: data type lengths [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

I'll look those up again, I've been reading these one at a time so its been
a few weeks since ive been through the others... the one I am on now is is
ISBN-13: 978-0-7356-2271-5 Page 113 last paragraph...

The wierd thing I think the eBook versions that come with the books are out
of date... I just went looking in the print book for that quote and its not
there on that page... but the ebook has it there... maybe they just didnt
update the PDF ebooks?

The other error was in T-SQL Programming INSIDE Microsoft SQL SERVER 2005
have to find that page though i dont have the ebook on hand for it but i
know it was there in the ebook cant seem to find it in print though... maybe
the ebooks are just messed up



"Aaron Bertrand [SQL Server MVP]" wrote in message

>> Yeah figured its wrong when I read that something just didn't feel right
>> because I new I've entered that number as number of chars in the past for
>> nvarchar... but I just found 3 other books on my shelf by MS Press that
>> say the same thing.... is anyone proofing these books?....
>
> Book titles, page numbers? I have plenty of MS Press books on my shelf.
> I don't have the book you originally mentioned though.
>
 >> Stay informed about: data type lengths 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jun 15, 2005
Posts: 632



(Msg. 11) Posted: Tue Feb 12, 2008 11:13 am
Post subject: Re: data type lengths [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Sounds backward to me... it is much easier (never mind financially feasible)
for them to update the PDF than a printed book. Especially if you already
own the book... it's not like a recall on a car. Smile

Of course just like a printed book, they can't update a PDF you downloaded a
long time ago (or that was distributed on the CD with the book).





"Smokey Grindel" wrote in message

> I'll look those up again, I've been reading these one at a time so its
> been a few weeks since ive been through the others... the one I am on now
> is is ISBN-13: 978-0-7356-2271-5 Page 113 last paragraph...
>
> The wierd thing I think the eBook versions that come with the books are
> out of date... I just went looking in the print book for that quote and
> its not there on that page... but the ebook has it there... maybe they
> just didnt update the PDF ebooks?
>
> The other error was in T-SQL Programming INSIDE Microsoft SQL SERVER 2005
> have to find that page though i dont have the ebook on hand for it but i
> know it was there in the ebook cant seem to find it in print though...
> maybe the ebooks are just messed up
 >> Stay informed about: data type lengths 
Back to top
Login to vote
Smokey Grindel

External


Since: Dec 11, 2006
Posts: 39



(Msg. 12) Posted: Tue Feb 12, 2008 11:31 am
Post subject: Re: data type lengths [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

yep.. wish they'd have an updated download site or something we could get
revisions for... the books say where to submit corrections to in them dont
they? kinda remember MS press books having somewhere to do that..

"Aaron Bertrand [SQL Server MVP]" wrote in message

> Sounds backward to me... it is much easier (never mind financially
> feasible) for them to update the PDF than a printed book. Especially if
> you already own the book... it's not like a recall on a car. Smile
>
> Of course just like a printed book, they can't update a PDF you downloaded
> a long time ago (or that was distributed on the CD with the book).
>
>
>
>
>
> "Smokey Grindel" wrote in message
>
>> I'll look those up again, I've been reading these one at a time so its
>> been a few weeks since ive been through the others... the one I am on now
>> is is ISBN-13: 978-0-7356-2271-5 Page 113 last paragraph...
>>
>> The wierd thing I think the eBook versions that come with the books are
>> out of date... I just went looking in the print book for that quote and
>> its not there on that page... but the ebook has it there... maybe they
>> just didnt update the PDF ebooks?
>>
>> The other error was in T-SQL Programming INSIDE Microsoft SQL SERVER 2005
>> have to find that page though i dont have the ebook on hand for it but i
>> know it was there in the ebook cant seem to find it in print though...
>> maybe the ebooks are just messed up
>
>
 >> Stay informed about: data type lengths 
Back to top
Login to vote
Tom Cooper

External


Since: Aug 23, 2006
Posts: 147



(Msg. 13) Posted: Tue Feb 12, 2008 1:06 pm
Post subject: Re: data type lengths [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

And, while you gave does show how many characters can be stored in an
nvarchar(10) column, when you are actually using them, don't forget the N
for the nvarchar constants, eg,
insert #t(c,n) values('1234567890',N'ÁÂ×ÇÄÅÖÚÉË')

Tom

"Alex Kuznetsov" wrote in message

On Feb 12, 10:31 am, "Smokey Grindel" wrote:
> yep.. wish they'd have an updated download site or something we could get
> revisions for... the books say where to submit corrections to in them dont
> they? kinda remember MS press books having somewhere to do that..
>
> "Aaron Bertrand [SQL Server MVP]" wrote in
> messagenews:ulxb1JZbIHA.3484@TK2MSFTNGP06.phx.gbl...
>
> > Sounds backward to me... it is much easier (never mind financially
> > feasible) for them to update the PDF than a printed book. Especially if
> > you already own the book... it's not like a recall on a car. Smile
>
> > Of course just like a printed book, they can't update a PDF you
> > downloaded
> > a long time ago (or that was distributed on the CD with the book).
>
> > "Smokey Grindel" wrote in message
> >
> >> I'll look those up again, I've been reading these one at a time so its
> >> been a few weeks since ive been through the others... the one I am on
> >> now
> >> is is ISBN-13: 978-0-7356-2271-5 Page 113 last paragraph...
>
> >> The wierd thing I think the eBook versions that come with the books are
> >> out of date... I just went looking in the print book for that quote and
> >> its not there on that page... but the ebook has it there... maybe they
> >> just didnt update the PDF ebooks?
>
> >> The other error was in T-SQL Programming INSIDE Microsoft SQL SERVER
> >> 2005
> >> have to find that page though i dont have the ebook on hand for it but
> >> i
> >> know it was there in the ebook cant seem to find it in print though...
> >> maybe the ebooks are just messed up

see for yourself, and trust your own eyes more than any book:

create table #t(c varchar(10), n nvarchar(10))
insert #t(c,n) values('1234567890','1234567890')
--------------
success

insert #t(c,n) values('1234567890a','1234567890')
-----------------------
Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.
The statement has been terminated.

insert #t(c,n) values('1234567890','1234567890a')
--------------
Msg 8152, Level 16, State 4, Line 1
String or binary data would be truncated.
The statement has been terminated.

insert #t(c,n) values('1234567890','ÁÂ×ÇÄÅÖÚÉË')
--------------
success

insert #t(c,n) values('1234567890','ÁÂ×ÇÄÅÖÚÉËÌ')
---------------------------------------
Msg 8152, Level 16, State 4, Line 1
String or binary data would be truncated.
The statement has been terminated.
 >> Stay informed about: data type lengths 
Back to top
Login to vote
Kalen Delaney

External


Since: Oct 27, 2003
Posts: 226



(Msg. 14) Posted: Tue Feb 12, 2008 3:13 pm
Post subject: Re: data type lengths [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

However, that is not how the publisher does it. They will fix errors in the
printed book when they do a reprint, but it's not part of their scheduled
maintenance to regenerate the e-books.

FWIW, Inside SQL Server: The Storage Engine does not make this mistake. Wink

Itzik has a list of errors posted at www.InsideTSQL.com, but I couldn't find
this error listed there.

--
HTH
Kalen Delaney, SQL Server MVP
www.InsideSQLServer.com
http://blog.kalendelaney.com


"Aaron Bertrand [SQL Server MVP]" wrote in message

> Sounds backward to me... it is much easier (never mind financially
> feasible) for them to update the PDF than a printed book. Especially if
> you already own the book... it's not like a recall on a car. Smile
>
> Of course just like a printed book, they can't update a PDF you downloaded
> a long time ago (or that was distributed on the CD with the book).
>
>
>
>
>
> "Smokey Grindel" wrote in message
>
>> I'll look those up again, I've been reading these one at a time so its
>> been a few weeks since ive been through the others... the one I am on now
>> is is ISBN-13: 978-0-7356-2271-5 Page 113 last paragraph...
>>
>> The wierd thing I think the eBook versions that come with the books are
>> out of date... I just went looking in the print book for that quote and
>> its not there on that page... but the ebook has it there... maybe they
>> just didnt update the PDF ebooks?
>>
>> The other error was in T-SQL Programming INSIDE Microsoft SQL SERVER 2005
>> have to find that page though i dont have the ebook on hand for it but i
>> know it was there in the ebook cant seem to find it in print though...
>> maybe the ebooks are just messed up
>
>
 >> Stay informed about: data type lengths 
Back to top
Login to vote
--CELKO--

External


Since: Apr 17, 2007
Posts: 417



(Msg. 15) Posted: Tue Feb 12, 2008 3:19 pm
Post subject: Re: data type lengths [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Standard SQL has both byte and character length functions for things
like this.
 >> Stay informed about: data type lengths 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Date Only Data Type - I would like to have a field in a table that is a Date only data type instead of the datetime. Could someone offer any advice on this in SQL Server 2005. Thanks

Changing data type on a column - I need to change the datatype of a column from int to bigint. The column is the identity column of the table and the table does have about 3 million records in it. How can I do this? I'm trying to use the following statement but keep getting an erro...

SQL data type question - I'm looking at a conversion table between sql data types and .net data types. I don't understand what a few of the sql datatypes are and where/how they would be used in sql server. varbinary(1), binary(1) = byte, Byte[] and nvarchar(1), nchar(1) =....

Image data type and text - I created a database with a single table with one column defined with the Image data type. I inserted text into the table and looked at the raw data contents in the single column. Looked nothing like my original text as to be expected. Is there a way....

reading image data type - If someone put text in an image data type, is there a way to search that column for specific data (full-text indexing would NOT be on the column)? In particular, it appears the Content column of the tbl_Content table in the TfsVersionControl..
   Database Forums (Home) -> Programming All times are: Pacific Time (US & Canada)
Goto page 1, 2
Page 1 of 2

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]