PostgreSQL BIT_LENGTH() Function
The PostgreSQL BIT_LENGTH() function returns the length of the specified string in bits.
Syntax
BIT_LENGTH(string)
Parameters
string |
Required. Specify the string to return the length for. |
Return Value
Returns the length of the specified string in bits.
Example 1:
The example below shows the usage of BIT_LENGTH() function.
SELECT BIT_LENGTH('Learning SQL is FUN!'); Result: 160 SELECT BIT_LENGTH(NULL); Result: NULL SELECT BIT_LENGTH(''); Result: 0 SELECT BIT_LENGTH(' '); Result: 8 SELECT BIT_LENGTH('SQL Tutorial'); Result: 96
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 in bits.
SELECT *, BIT_LENGTH(City) AS BIT_LENGTH_Value FROM Employee;
The query will produce the following result:
EmpID | Name | City | Age | BIT_LENGTH_Value |
---|---|---|---|---|
1 | John | London | 25 | 72 |
2 | Marry | New York | 24 | 96 |
3 | Jo | Paris | 27 | 60 |
4 | Kim | Amsterdam | 30 | 108 |
5 | Ramesh | New Delhi | 28 | 108 |
6 | Huang | Beijing | 28 | 84 |
❮ PostgreSQL Functions