SQLite LENGTH() Function
The SQLite LENGTH() function returns the following:
- if the argument is a string, it returns the total number of characters in the string.
- if the argument is a blob value, it returns the number of bytes in the blob.
- if the argument is NULL, it returns NULL.
- if the argument is a numeric value, it returns the length of a string representation of the argument.
Syntax
LENGTH(X)
Parameters
X |
Required. Specify the value to return the length for. |
Return Value
Returns the length of the X.
Example 1:
The example below shows the usage of LENGTH() function.
SELECT LENGTH('12345'); Result: 5 SELECT LENGTH('ABCDE'); Result: 5 SELECT LENGTH(12345); Result: 5 SELECT LENGTH('AlphaCodingSkills'); Result: 17 SELECT LENGTH('Alpha Coding Skills'); Result: 19 SELECT LENGTH(NULL); Result: NULL SELECT LENGTH(''); Result: 0 SELECT LENGTH(' '); Result: 1
Example 2:
Consider a database table called Employee with the following records:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
The statement given below can be used to get the length of records of City column.
SELECT *, LENGTH(City) AS LENGTH_Value FROM Employee;
The query will produce the following result:
EmpID | Name | City | Age | LENGTH_Value |
---|---|---|---|---|
1 | John | London | 25 | 6 |
2 | Marry | New York | 24 | 8 |
3 | Jo | Paris | 27 | 5 |
4 | Kim | Amsterdam | 30 | 9 |
5 | Ramesh | New Delhi | 28 | 9 |
6 | Huang | Beijing | 28 | 7 |
❮ SQLite Functions