• subscribe
July 24, 2003 12:00 AM

Counting Blank Spaces

SQL Server Pro
InstantDoc ID #39445

Why does the command SELECT LEN('123' + ' '), which counts the number of characters in a field, return three when it should return eight? Three characters plus five blank spaces is eight characters in total. How can I count the blank spaces?

When in doubt about unexpected behavior, read SQL Server Books Online (BOL). The BOL entry for the LEN() function says that it ignores trailing blanks. Many people make the mistake of assuming the LEN() function will count trailing blanks because similar functions in other programming languages do. To get the answer you're looking for, use the DATALENGTH() function instead:

SELECT DATALENGTH('123' + 
'    ')


ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here