SQL Functions   «Prev  Next»
Lesson 5SQL LENGTH function
ObjectiveWrite a query that combines several SQL Functions which were introduced earlier in this module.

Describe the SQL Length Function

The ability to measure the length of a string is more important in SQL than it first appears. Developers use string-length functions when they need to inspect text values, validate input, filter rows by size, sort output by the length of a field, or combine several SQL features into one useful query. A lesson on LENGTH(), LEN(), and CHARACTER_LENGTH() is therefore not just about memorizing another function name. It is about learning how SQL evaluates textual data in practical situations.

This lesson explains the role of SQL string-length functions, how those functions differ across database systems, and how they can be combined with clauses such as SELECT, WHERE, and ORDER BY. It also explains an important distinction: some functions focus on the number of bytes used to store a string, while others focus on the number of visible characters in the string. That distinction matters when a database stores Unicode or other multi-byte character data.

The topic connects naturally to other functions you have already studied in this module. Once you understand how SQL can measure the size of a string, you can begin combining that measurement with filtering logic, aliases, sorting rules, and other expressions to write richer and more useful queries.

Why String Length Matters in SQL

Knowing the length of a string helps with several common SQL tasks. A developer may want to see whether a value is too short, whether a title exceeds a certain threshold, or whether a name fits a presentation rule. String length also becomes useful when formatting output, aligning displayed values, validating imported data, or identifying rows whose text values deserve closer inspection.

For example, a reporting query may need to find titles shorter than a target length so they fit within a fixed-width display area. A data-review query may look for suspiciously short names that suggest incomplete data entry. A developer may also want to sort rows by the size of a text field in order to review the shortest or longest values first.

So although string length sounds like a small technical detail, it supports real design and querying decisions. SQL length functions help convert text into something measurable, and that makes string data easier to analyze.

Common Function Names: LEN, LENGTH, and CHARACTER_LENGTH

One important lesson in this topic is that database products do not always use the same function names. Depending on the SQL platform, you may see a string-length function written as LEN(), LENGTH(), or CHARACTER_LENGTH(). These functions all relate to measuring string size, but the exact name and behavior depend on the database engine.

For example, Microsoft SQL Server commonly uses LEN():

SELECT LEN(MyColumn)
FROM MyTable;

Many other systems use LENGTH():

SELECT LENGTH(MyColumn)
FROM MyTable;

Standard SQL also provides CHARACTER_LENGTH(), which makes the intent especially clear because it explicitly counts characters:

SELECT CHARACTER_LENGTH(MyColumn)
FROM MyTable;

This is why cross-platform awareness matters. A query that works in one SQL engine may need adjustment in another. When you move between products, always verify which function name is supported and what that function actually returns.

LENGTH and LEN as General String-Length Functions

The general idea behind LEN() and LENGTH() is simple: these functions return a numeric value representing the length of a string expression. The expression may be a column, a literal, or the result of another function. That makes these functions easy to combine with other SQL features.

If your database supports one of these functions, the result is usually returned in a derived expression column unless you provide an alias. For example:

SELECT LENGTH(MyColumn) AS value_length
FROM MyTable;

Without the alias, the query still works, but the derived column may receive a less readable heading. Aliasing becomes especially useful when a query contains several calculated expressions.

The legacy page notes that some engines support these functions differently. That remains an important warning. Developers should not assume that every product implements string-length logic in precisely the same way. The function may be available under a different name, and its treatment of empty strings, spaces, or multi-byte characters may vary by platform.

CHARACTER_LENGTH and the Difference Between Characters and Bytes

The most important conceptual point in this lesson is the difference between byte length and character length. A byte measures storage size, while a character measures textual units as the user sees them. If a string contains only simple single-byte characters, the two values may appear the same. But when the data includes Unicode or multi-byte characters, the number of bytes used for storage may be greater than the number of characters in the string.

This is why CHARACTER_LENGTH() deserves special attention. It is intended to return the number of characters in a string, not merely the number of bytes used to store it. For many text-processing tasks, that makes CHARACTER_LENGTH() the clearer and safer function.

The function’s important characteristics can be summarized as follows:

  1. CHARACTER_LENGTH() returns an integer greater than or equal to zero.
  2. It counts characters rather than bytes.
  3. A multi-byte or Unicode character still represents one character for counting purposes.
  4. If the argument is NULL, the function returns NULL.

This distinction matters in multilingual or Unicode-aware systems. A developer who wants the visible character count of a value should usually think in terms of CHARACTER_LENGTH(), not only raw byte storage.

Using CHARACTER_LENGTH in a SELECT Statement

A straightforward use of CHARACTER_LENGTH() is to display the size of a string for each row in a table. This is useful for inspection, reporting, and data review. The existing lesson includes an example that lists the lengths of authors’ first names:

SELECT au_fname,
  CHARACTER_LENGTH(au_fname) AS "Len"
FROM authors;

This query returns each author’s first name along with a derived column showing the number of characters in that name. The alias "Len" makes the output easier to read. This is a small but useful example of how SQL functions and aliases can work together in the same statement.

Queries like this are helpful when you want to inspect the shape of your data. You can see whether names are short, long, empty, or inconsistent. In a real database environment, that type of quick inspection often helps with both data validation and query design.

Nulls, Empty Strings, and Practical Caution

The current lesson correctly notes that if the argument passed to CHARACTER_LENGTH() is NULL, the function returns NULL. That behavior is consistent with how many SQL functions propagate null values. A missing value remains missing; the function does not invent a replacement.

Empty strings deserve a little care. In many systems, the length of an empty string is zero. However, database products do not all behave identically, especially when empty strings, blank strings, and null values interact with vendor-specific rules. That is another reason to verify behavior on the database engine you are actually using.

For a learner, the safe practical idea is this: do not assume that every DBMS treats empty strings and nulls exactly the same way. Test the behavior when portability matters.

Combining String-Length Functions with WHERE and ORDER BY

The lesson objective is not merely to identify a string-length function. It is to write a query that combines several SQL features learned earlier in the module. This is where the topic becomes more interesting. Once a length function appears inside a query, it can be combined with filtering and sorting logic.

The existing page includes a strong example:

SELECT title_name,
  CHARACTER_LENGTH(title_name) AS "Len"
FROM titles
WHERE CHARACTER_LENGTH(title_name) < 30
ORDER BY CHARACTER_LENGTH(title_name) ASC;

This query does several useful things at once:

  • It selects the title text.
  • It computes the character length of each title.
  • It filters the rows so that only titles shorter than 30 characters are returned.
  • It sorts the result by title length in ascending order.

That is exactly the kind of query structure this lesson should teach. The learner can see that a function is not isolated from the rest of the SQL statement. Instead, the function becomes part of the overall logic of the query.

Why Function Composition Matters

One of the bigger lessons in Advanced SQL is that useful queries often combine multiple ideas rather than using one feature at a time. A length function may begin as a simple expression in the SELECT list, but its real power appears when it is combined with other parts of the statement.

In the example above, CHARACTER_LENGTH() works together with:

  • SELECT to project the original value and the derived measurement,
  • AS to label the derived column,
  • WHERE to restrict the rows by size,
  • ORDER BY to sort the rows based on that computed value.

This kind of function composition is what makes SQL expressive. Instead of issuing several separate commands or performing manual calculations outside the database, you can let the query engine calculate, filter, and sort in one statement.

String-Length Functions as Data Review Tools

String-length functions are especially useful for data review. A developer may use them to identify values that are suspiciously short or unexpectedly long. In customer data, for example, a two-character last name may be legitimate, but a one-character city name might deserve closer inspection. In a titles table, sorting by character length makes it easy to spot unusually brief or unusually long entries.

This is why the lesson’s title examples are effective. They show that string-length functions are not only theoretical tools. They help you ask practical questions of your data:

  • Which titles are shorter than a target length?
  • Which names appear empty or incomplete?
  • How long are the strings being stored in a given column?
  • Can output be formatted more clearly when the length is known?

Even simple measurement functions become valuable once they are connected to real tasks such as validation, reporting, and exploration.

Portability and DBMS Awareness

Another theme in this lesson is portability. SQL is a common language, but its implementations vary. One system may prefer LEN(), another may use LENGTH(), and another may distinguish carefully between byte-oriented and character-oriented functions. That is why vendor documentation matters.

The learner should take away a balanced view:

  • The overall concept of string length is common across systems.
  • The exact function name may vary.
  • The meaning of “length” may differ when character encoding becomes important.
  • Testing on the target platform is always wise.

This DBMS awareness is part of writing stronger SQL. A good query is not only syntactically correct; it is also appropriate for the system on which it will run.

Preparing for the Exercise

At this stage, you should be ready to approach the exercise with a broader understanding of what SQL string-length functions do. You have seen that string length can be measured for display, filtering, sorting, and validation. You have also seen why CHARACTER_LENGTH() is especially useful when the goal is to count characters rather than bytes.

As you work through the exercise, focus on the following questions:

  1. Which function name is appropriate for the SQL engine being used?
  2. Am I trying to count bytes or characters?
  3. How can I combine the function with aliases, filters, or ordering logic?
  4. How does the derived length value improve the usefulness of the query?

Those questions move the lesson beyond memorizing syntax. They help you think like a SQL developer who uses functions as building blocks in larger query designs.

Sql Function - Exercise

Complete this exercise to practice mixing and matching functions.

SQL Function - Exercise

SEMrush Software Target 5SEMrush Software Banner 5