SQL Server String Length: LEN vs DATALENGTH

Formulas & functions · Updated November 23, 2026

The main function to get a string’s length in SQL Server is LEN(), which returns the number of characters in a string expression, excluding trailing spaces. For the number of bytes, use DATALENGTH(). This article explains both functions, their differences, and how to choose the right one for your queries.

Background: Why String Length Matters in SQL Server

Knowing the length of a string is a fundamental operation in SQL Server, used for data validation, formatting, and logic in queries. For example, you might need to check if a phone number has the correct number of digits, or you might want to filter out records where a description is too short. SQL Server provides two primary functions for this: LEN() and DATALENGTH(). Understanding the difference between them is crucial because they answer different questions: LEN() tells you the number of characters, while DATALENGTH() tells you the number of bytes. This distinction becomes critical when dealing with Unicode data or when you need to know the storage footprint.

How LEN() Works

LEN() returns an integer representing the number of characters in a string, excluding trailing spaces. It does not count leading spaces. Here’s the syntax:

LEN ( string_expression )

For example:

SELECT LEN('Hello World') AS Length; -- Returns 11
SELECT LEN(' Hello ') AS Length; -- Returns 7 (leading spaces counted, trailing spaces ignored)

Note that LEN() ignores trailing spaces. If you need to count them, you must use DATALENGTH() or concatenate a character. For instance, LEN('Hello ' + 'x') - 1 would give you the length including the trailing space.

How DATALENGTH() Works

DATALENGTH() returns the number of bytes used to represent the expression. For a string, this depends on the data type. For varchar, each character uses 1 byte, so DATALENGTH() equals the number of characters. For nvarchar, each character uses 2 bytes (in most collations), so DATALENGTH() is twice the number of characters. This function does not ignore trailing spaces; it counts every byte.

SELECT DATALENGTH('Hello') AS Bytes; -- Returns 5 for varchar, 10 for nvarchar

To get the character count of an nvarchar string including trailing spaces, you can use DATALENGTH(expression) / 2 if the collation uses 2 bytes per character, but be careful with supplementary characters (those outside the Basic Multilingual Plane) which may use 4 bytes.

LEN vs DATALENGTH: Which One to Use?

Choosing between LEN() and DATALENGTH() depends on your goal:

  • Character count for display or validation: Use LEN(). It gives you the human-readable length, ignoring trailing spaces, which is usually what you want for checking input lengths.
  • Byte count for storage or network transfer: Use DATALENGTH(). It tells you how much space the string occupies, which is important for indexing, performance, or when calculating payload sizes.
  • When trailing spaces matter: Use DATALENGTH() (or a workaround with LEN()) because LEN() strips them.

For example, if you have a column defined as nvarchar(50) and you want to ensure a value does not exceed 50 characters, LEN() is appropriate. But if you’re calculating the total size of a row for a data page, you’d use DATALENGTH().

Common Problems and Misconceptions

A frequent misconception is that LEN() counts trailing spaces. It does not. This can lead to unexpected results when comparing strings or validating fixed-length fields. Another issue is using LEN() on varchar(max) or nvarchar(max) columns—it works fine, but be aware of performance implications on large data. Also, LEN() returns NULL if the input is NULL, so you may need to use ISNULL() or COALESCE() in your logic.

Another common problem: when using DATALENGTH() on text or ntext data types (deprecated), it returns the number of bytes, but those types are being phased out. Always use varchar(max) or nvarchar(max) instead.

Alternatives and Related Functions

SQL Server also offers other string functions that may be useful:

  • CHARINDEX() – finds the position of a substring.
  • SUBSTRING() – extracts a part of a string.
  • REPLACE() – replaces occurrences of a substring.
  • REPLICATE() – repeats a string a specified number of times.
  • LEFT() and RIGHT() – extract characters from the left or right.

For example, to check if a string is longer than 10 characters, you could use LEN(column) > 10. To get the last 3 characters, use RIGHT(column, 3). These functions often work together with LEN() in more complex expressions.

Performance and Best Practices

Using LEN() or DATALENGTH() in a WHERE clause can prevent index usage. For example, WHERE LEN(LastName) > 5 will force a scan because the length is computed on the fly. To optimize, consider storing the length in a separate column or using a computed column with an index. Also, be mindful of implicit conversions: if you pass a number to LEN(), it converts it to a string first, which may be unexpected.

When working with Unicode, remember that LEN() counts characters, not bytes, so for an nvarchar column, LEN() gives the number of characters, which is what you usually want. DATALENGTH() gives bytes, which is twice that for typical Unicode strings.

Frequently Asked Questions

Why does LEN() ignore trailing spaces?

This is by design in SQL Server. The rationale is that trailing spaces are often insignificant in comparisons and display, and ignoring them simplifies length checks. If you need to include them, use DATALENGTH() or append a sentinel character.

How do I get the length of a string including trailing spaces?

Use DATALENGTH() for byte count, or for character count, you can do LEN(string + 'x') - 1. For example, LEN('Hello ' + 'x') - 1 returns 6, which includes the trailing space.

What is the difference between LEN and CHAR_LENGTH?

In SQL Server, LEN() is the function to use. CHAR_LENGTH() is not a SQL Server function; it’s used in other databases like MySQL. SQL Server uses LEN() for character count.

Can I use LEN() on a column of type TEXT?

Yes, you can use LEN() on text and ntext columns, but these types are deprecated. It’s recommended to use varchar(max) or nvarchar(max) instead.

Does LEN() count leading spaces?

Yes, LEN() counts leading spaces. For example, LEN(' Hello') returns 7. Only trailing spaces are ignored.

How do I find the length of a string in a CASE statement?

You can use LEN() inside a CASE expression: CASE WHEN LEN(column) > 10 THEN 'Long' ELSE 'Short' END. This works fine.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.