Introduction to PostgreSQL LENGTH()
PostgreSQL length() function is used to find the length of string, which we have used in length function. Length function is used to find the number of character from string, length function is also used with column name to find length of column row. Length function is used in PostgreSQL 8.4, to PostgreSQL 12 versions. We have used cast with length functions to find length of integer numbers, cast is very useful to find length of integer string in PostgreSQL. PostgreSQL length function is very important and useful to find the length of a string.
Syntax
Below is the syntax of PostgreSQL length function are as follows.
- Length (String (String name which we have used to find length.));
- Select length (”); (We have used empty string using length function in PostgreSQL.)
- Select length (NULL);(We have used null string using length function in PostgreSQL.)
- Select length (‘ ‘); (We have used space string using length function in PostgreSQL.)
- Select column_name1, Column_name2, LENGTH (Column_name)from table_name order by column_name;
Parameters
Below is the parameter description of above syntax are as follows.
- Length: Length function is used to find length of string. We have used this function to find column rows length in PostgreSQL. Length function is very important and useful in PostgreSQL.
- Null: We have also used null string in length function. The result of null string is null.
- Length (‘ ‘): We have also used space string in length function. The result of space string is how many space we have used in string.
- Length (”): We have also used empty string in length function. The result of empty string is zero.
- String: String is defines as which string or word we have used to find the length using length function in PostgreSQL.
- Select: We have used select to select length of string in PostgreSQL. We have also used select to select column name from table and find the length of column.
- Column 1 to column N: Column name used to find character length of column by using length function.
- Order by: Order by clause is used in length function to arrange the length of column in ascending or descending order.
- Table name: Table name is used to select the column name and by using column name find the length of column.
How LENGTH() function works in PostgreSQL?
Below is the working of length() function.
- Length function is used to find the length of string which we have used in length function. Length function is very important and useful in PostgreSQL to calculate the length of string.
- Length function is used to find the number of character from string.
- Length function is also used with column name to find length of column row. We have also used order by in length function to display character count of string in ascending or descending order.
- We have used cast with length functions to find length of integer numbers.
- Cast in length function is very useful to find length of integer string in PostgreSQL.
- If we have used null value as a string in length function the result of null value string is always null.
- If we have used space value as a string in length function the result of space value string is how many space we have used in string,it will shows in result length of string that how many space we have used in space string.
- The length of capital and small letter is same in length function. In below example we have use “ABCDEFGH” and “abcdefgh” string with length function result of both string are same.
Example:
select length ('ABCDEFGH');
- If we have used empty value as a string in length function the result of empty value string is always zero.
- PostgreSQL length function is accept the string as a parameter value. Length function will display the length of following data types are as follows.
- Char
- Varchar
- Text
- Length function in PostgreSQL will returns a number of character that we have used in string.
Example to Implement LENGTH() Function in PostgreSQL
Below is the example of length function.
Example #1
In below example we have finding the length of null string. The result of null string is null.
select length (NULL);
Example #2
In below example we have finding the length of empty string. The result of empty string is zero.
select length ('');
Example #3
In below example we have finding the length of empty string. The result of empty string is 3 because we give three spaces.
select length (' ');
Example #4
In below example we have finding length of number string. We need to use cast function to find length of integer string.
SELECT LENGTH (CAST('12345' as TEXT));
We have using product table to describe example of length function in PostgreSQL. Below is the table description of product table.
Select * from product;
Example #5
Below example shows find the length of column data from product table.
select prod_id, prod_name, length(prod_name)AS prod_name_length, length(delivery_address) from product order by prod_name_length ASC;
Conclusion
PostgreSQL length function is used to find the length of string, which we have usedin length function. It is accept the string as a parameter value or a string. A string value using length function accept the string of char, varchar and text data types.
Recommended Articles
This is a guide to PostgreSQL LENGTH(). Here we discuss an introduction to PostgreSQL LENGTH(), how does it work along with query examples. You may also have a look at the following articles to learn more –