Length Function
Next: SQL Replace
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_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:
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 |