LEN()

by Allan Svelmøe Hansen December 02, 2008 16:21

Just a quick notice about the LEN function in SQL Server 2000 and 2005 (at least, haven't looked at 2008 yet).
But an often overlooked “feature” of LEN is that the function trims trailining spaces, so a LEN('a ') will return 1.
However if you reverse it and use LEN(' a') it’ll return 2.

This is something which can cause problems/issues at times, if not paying attention to the documentation.

Bookmark and Share DotnetKicks dotnetshoutout

Tags:

SQL

Comments

12/11/2008 10:30:37 AM #

Actually, in SQL Server 2008 it behaves the same.

Regards,
Pinal Dave ( http://blog.sqlauthority.com )

Pinal Dave India

1/12/2009 2:16:53 PM #

I believe this is to handle fixed-length char types, like char(150).

Otherwise they would always return the field length.

BigBen United States

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.1.0
Theme by Mads Kristensen | Modified by Mooglegiant

About:
Allan Svelmøe Hansen

My real name is Allan Svelmøe Hansen.
I live in Denmark, where I work as a developer for hedal:kruse:brohus using SQL Server and the .NET framework since 2004.
My primary fields of expertise is back-end data integration, database design and optimization.


       View Allan Svelmøe Hansen's profile on LinkedIn     

Disclaimer

The opinions expressed herein are my own personal opinions and thoughts and does not represent my employers view in any way, nor are my results guaranteed for all situations.
Content is presented "as is", with no warranty.