SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SUBSTRING length parameter: byte vs character length...


SUBSTRING length parameter: byte vs character length...

Author
Message
ducon
ducon
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1169 Visits: 286
Hi,

I have a question regarding the documentation in BOL 2005 for the SUBSTRING function.

I've been toying around with nvarchar, and substring, and in the BOL, the SUBSTRING entry states that:


length :
Is a positive integer that specifies how many characters or bytes of the expression will be returned. If length is negative, an error is returned. length can be of type bigint


It's just not very clear to me what the expression "characters or bytes" means... I'd want to know which one it is to be honest...

For example:


declare @a nvarchar(10)
set @a = N'üThis is a test'
if datalength(@a) >= 20 begin
set @a = substring(@a, 1, 7) + N'...'
end

select @a



I would have thought that my substring function would need 14 bytes as a length parameter, not 7 characters in order to truncate my expression. Could this be related to my regional settings?

Thanks,

Greg
Lowell
Lowell
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97576 Visits: 41185
yeah it seems misleading.
I would say it is ALWAYS Characters. it's just coincidence that when you have a varchar, the bytes=# chars. i think that was what they were trying to say in BOL.

when it's nvarchar, the bytes are doubles, but it's still characters the function expects.

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
ducon
ducon
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1169 Visits: 286
That's what I figured as well but I just wanted to make sure that's what it meant, and not that it depended on some regional/collation setting somewhere that it would either use byte or char as a unit.

Thanks!

Greg
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search