TSQL: Get the length of String including/excluding trailing spaces

Categories: Database; Tagged with: ; @ January 16th, 2013 11:53

LEN (Transact-SQL)
Returns the number of characters of the specified string expression, excluding trailing blanks.
Note: To return the number of bytes used to represent an expression, use the DATALENGTH function.

http://msdn.microsoft.com/en-us/library/ms190329(SQL.90).aspx

DATALENGTH (Transact-SQL)

Returns the number of bytes used to represent any expression.

 

SQLs:

DECLARE @STR VARCHAR(32);
SET @STR = ’12    ‘
SELECT LEN(@STR)  — 2
SELECT DATALENGTH(@STR); –6

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.