SQL > SQL String Functions > Length Function

The Length function in SQL is used to get the length of a string. This function has a different name for different databases:

  • MySQL: LENGTH( )
  • Oracle: LENGTH( )
  • SQL Server: LEN( )
SQL LENGTH (or LEN in SQL Server) returns the number of characters in a string—useful for data validation, filtering by string size, and computed columns. Note that SQL Server's LEN() does not count trailing spaces, while MySQL and Oracle's LENGTH() does.

Syntax

The syntax for the Length function is as follows:

Length (str)

The length of the string str is returned.

Examples

We use the following table for our examples.

Table Geography

 Region_Name  Store_Name 
 East  Boston 
 East  New York 
 West  Los Angeles 
 West  San Diego 

Example 1

SELECT Length (Store_Name) 
FROM Geography 
WHERE Store_Name = 'Los Angeles';

Result:

Length (Store_Name)
11

Example 2

SELECT Region_Name, Length (Region_Name) 
FROM Geography;

Result:

Region_Name Length (Region_Name)
East 4
East 4
West 4
West 4

Frequently Asked Questions

Q: What does the SQL LENGTH function do?
A: LENGTH returns the number of characters in a given string. It is useful for data validation, filtering rows by string size, or computing derived values.

Q: What is the difference between LENGTH and LEN in SQL?
A: LENGTH() is used in MySQL and Oracle. SQL Server uses LEN() instead. Both return the number of characters in a string, but LEN in SQL Server trims trailing spaces before counting.

Q: Does SQL LENGTH count spaces?
A: Yes, LENGTH counts all characters including spaces between words. However, SQL Server's LEN() does not count trailing spaces.

Q: How can I find rows where a string column exceeds a certain length?
A: Use LENGTH in a WHERE clause: SELECT * FROM table WHERE LENGTH(column_name) > 10;

Next: SQL Replace

This page was last updated on March 19, 2026.




Copyright © 2026   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact