Standard SQL Length Function
The Standard SQL function to determine the length of a string is LENGTH(). This function takes a string or bytes and returns the string.
The syntax is as shown below.:
Consider an example shown below:
The query above should return the length of the provided string. An example output is as shown below:
--------
2
NOTE: Keep in mind that the length here indicates the number of Unicode characters in the provided string.
You can check Unicode characters as shown below:
https://unicode.org/standard/standard.html
Extended String Length Function
Various database engines implemented and extended versions of the SQL Standard. One example is the string length function.
In database engines such as MySQL, this function is renamed to LEN. However, the usage is similar to the one shown above.
The syntax is as shown below:
The function takes the string as the argument and returns the number of characters in the string.
An example usage is as shown below:
This should return:
--------
2
You can also use the LENGTH (SQL Standard), and LEN(extended) functions to determine the length of strings in a column.
An example usage is as shown:
In the example above, we use a sample customer’s table to get the length of the first_name column.
The resulting set is as shown:
------------+--------
MARY | 4
PATRICIA | 8
LINDA | 5
BARBARA | 7
ELIZABETH | 9
Closing
In this article, we discuss how to determine the length of a string using the LENGTH function in Standard SQL and LEN in extended SQL version.