Length Function

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

  • MySQL: LENGTH( )
  • Oracle: LENGTH( )
  • SQL Server: LEN( )

The syntax for the Length function is as follows:

Length(str): Find the length of the string str.

Let's take a look at some examples. Assume we have the following table:

Table Geography

region_namestore_name
EastBoston
EastNew York
WestLos Angeles
WestSan Diego

Example 1:

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

Result:

11

Example 2:

SELECT region_name, Length(region_name)
FROM Geography;

Result:

region_nameLength(region_name)
East4
East4
West4
West4

Next: SQL Replace