Arogl Darthu's Blog

Entries from Wednesday, November 12. 2008

  • 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)
« previous page   (Page 1 of 1, totaling 1 entries)   next page »

Calendar

Back November '08 Forward
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 30

Archives

  • May 2012
  • April 2012
  • March 2012
  • Recent...
  • Older...

Categories

  • XML BizTalk
  • XML C#
  • 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