Data Value

  • I have a varchar(2) column.  If insert '  ' or '', I get same results.

    The length of both column is zero.  Please explain, why.

    Thanks

  • Varchar() trims blanks from the right of the data.  Ie. 'tom   ' is stored as 'tom' but '   tom' is '   tom'.

    Tom

  • There is actually a bit more to it depending upon the ANSI_PADDING settings...see this discussion thread:

    http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=19&messageid=226423#bm226578

  • A varchar will store spaces if they're part of the string that's put in the variable/field. It just won't pad with spaces to full length like char does.

    Note that LEN does an RTrim before computing length.

    DECLARE @str1 VARCHAR(10), @str2 VARCHAR(10)

    SELECT @str1 = 'abc       ', @str2 = 'abc'

    SELECT LEN(@str1) as Length1, LEN(@str2) AS Length2

    SELECT '[' + @str1 + ']' AS String1, '[' + @str2 + ']' AS String2

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

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

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