Length

suggest change

SQL Server


LEN is SQL Server. It does not count trailing spaces. Standard CHAR_LENGTH does.

SELECT LEN('Hello') -- returns 5

SELECT LEN('Hello '); -- returns 5

The DATALENGTH counts the trailing space.

SELECT DATALENGTH('Hello') -- returns 5

SELECT DATALENGTH('Hello '); -- returns 6

DATALENGTH returns the number of bytes in the stored value, which depends on the type. A varchar character is usually one byte and an nvarchar character is two.

DECLARE @str varchar(100) = 'Hello ' --varchar is usually an ASCII string, occupying 1 byte per char
SELECT DATALENGTH(@str) -- returns 6

DECLARE @nstr nvarchar(100) = 'Hello ' --nvarchar is a unicode string, occupying 2 bytes per char
SELECT DATALENGTH(@nstr) -- returns 12

Oracle


Syntax: Length ( char )

Examples:

SELECT Length('Bible') FROM dual; --Returns 5
SELECT Length('righteousness') FROM dual; --Returns 13
SELECT Length(NULL) FROM dual; --Returns NULL

See Also: LengthB, LengthC, Length2, Length4

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents