SUBSTRING length parameter: byte vs character length...

  • 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

  • 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!

  • 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

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply