Quirks of LEN

Arogl Darthu's Blog

  • Homepage

Nov 12: Quirks of LEN

As we all know, the LEN function in T-SQL returns the character length and at that ignores the trailing blanks... So, how to return the length including these trailing blanks? I experimented a little and found that all you have to do is CAST to the appropriate MAXed variant of your datatype. So just try the following:

DECLARE @l_nContentMax NVARCHAR(MAX)
,       @l_nContent    NVARCHAR(20)

SELECT @l_nContentMax = '1234567890     ' -- character length = 15
,      @l_nContent    = '1234567890     ' -- character length = 15

SELECT LEN(N'1234567890     ')                        AS Plain
,      LEN(CAST(N'1234567890     ' AS NVARCHAR(MAX))) AS MaxCast
,      LEN(@l_nContent)                               AS PlainVariable
,      LEN(@l_nContentMax)                            AS MaxVariable
 
This results in:
T-Sql Results

So... just cast your expression to NVARCHAR(MAX) or VARCHAR(MAX) where appropriate and you'll get the right length ;-)
Posted by Twan Jacobs Comments: (0) Trackbacks: (0)

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

No comments


Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 

Calendar

Back February '12
Mo Tu We Th Fr Sa Su
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29        

Archives

  • February 2012
  • January 2012
  • December 2011
  • Recent...
  • Older...

Categories

  • XML BizTalk
  • XML Daily Didst
  • XML Infra
  • XML SQL
  • XML WCF
  • XML WTF


All categories

Syndicate This Blog

  • XML RSS 2.0 feed
  • XML RSS 2.0 Comments

Blog Administration

Open login screen

Powered by

Serendipity PHP Weblog
 

Layout by Andreas Viklund | Serendipity template by Carl