PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL OCTET_LENGTH() Function



The PostgreSQL OCTET_LENGTH() function returns the length of the specified string (measured in bytes). This function counts a multi-byte character as more than one byte.

Note: To count a multi-byte character as a single character, LENGTH() function can be used.

Syntax

OCTET_LENGTH(string)

Parameters

string Required. Specify the string to return the length for.

Return Value

Returns the length of the specified string (measured in bytes).

Example 1:

The example below shows the usage of OCTET_LENGTH() function.

SELECT OCTET_LENGTH('12345');
Result: 5

SELECT OCTET_LENGTH('ABCDE');
Result: 5

SELECT OCTET_LENGTH('AlphaCodingSkills');
Result: 17

SELECT OCTET_LENGTH('Alpha Coding Skills');
Result: 19

SELECT OCTET_LENGTH(NULL);
Result: NULL

SELECT OCTET_LENGTH('');
Result: 0

SELECT OCTET_LENGTH(' ');
Result: 1

Example 2:

Consider a database table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

The statement given below can be used to get the length of records of City column.

SELECT *, OCTET_LENGTH(City) AS OCTET_LENGTH_Value FROM Employee;

The query will produce the following result:

EmpIDNameCityAgeOCTET_LENGTH_Value
1JohnLondon256
2MarryNew York248
3JoParis275
4KimAmsterdam309
5RameshNew Delhi289
6HuangBeijing287

❮ PostgreSQL Functions