Results 1 to 7 of 7

Thread: Strange Len function

  1. #1
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254

    Strange Len function

    Why is it so?

    select len("Test")
    --returns 4

    select len("Test ")
    --returns 4

    select len(" Test ")
    --returns 7

    why not all 4 ?
    why not 4,11,14?

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    I got 4, 4, 5. Seems system ignores trailing blanks.

  3. #3
    Join Date
    Nov 2002
    Location
    Chesapeake, VA
    Posts
    104
    When I run those statements I get different results. (double quotes failed for me).

    select len('Test')
    --returns 4

    select len('Test ')
    --returns 4

    select len(' Test ')
    --returns 5

    The reason I get the results I got is because I have ANSI_PADDING turned off.

    Sidney Ives

  4. #4
    Join Date
    Sep 2002
    Posts
    5,938
    I tried with ANSI_PADDING on but still got same result.

  5. #5
    Join Date
    Nov 2002
    Location
    Chesapeake, VA
    Posts
    104
    Works the same for me as well. So much for SQL doc.

    Sidney

  6. #6
    Join Date
    Mar 2003
    Location
    Woking, UK
    Posts
    152

    Function LEN in Books Online

    LEN
    Returns the number of characters, rather than the number of bytes, of the given string expression, excluding trailing blanks...

    ... and here is my tip ...

    select len('test ')
    , len(N'test ')
    , datalength('test ')
    , datalength(N'test ')/2
    --returns 4,4,5,5



    YuckFou

  7. #7
    Join Date
    Oct 2002
    Location
    queens
    Posts
    139
    Good tips, Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •