PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Functions / PostgreSQL LENGTH Function

PostgreSQL LENGTH Function

Summary: in this tutorial, we will show you various PostgreSQL length functions that return the number of characters or the number of bytes of a string.

PostgreSQL LENGTH function

The following illustrates the syntax of the length function:

1
LENGTH(string);

The length function accepts a string as a parameter. A string can be any of the following data types:

  • character or char
  • character varying or varchar
  • text

The length function returns the number of characters in the string.

PostgreSQL length function examples

See the following example of using the length function:

1
2
SELECT
LENGTH ('PostgreSQL Tutorial'); -- 19

Notice that a string can hold an empty string, which is not a null value.

1
2
SELECT
LENGTH (''); -- 0

It returns zero. However, a string that holds a space character:

1
2
SELECT
LENGTH (' '); -- 1

It returns 1.

If you pass a NULL value to the length function, it returns a NULL value.

1
2
SELECT
LENGTH (NULL); -- NULL

The following query gets the full names of customers in the customer table and uses the length function to get the number of characters in their names.

1
2
3
4
5
6
7
SELECT
first_name || ' ' || last_name AS name,
LENGTH (first_name || ' ' || last_name) len
FROM
customer
ORDER BY
len;

postgresql length function example

Sometimes, you may want to measure the length of a number instead of a string. In this case, you use type cast to convert the number into a string and use the length function as the following example:

1
2
SELECT
LENGTH (CAST(12345 AS TEXT)); --- 5

We often use the length function with other string functions such as replace, substring, etc., to manipulate string more efficiently. The following statement gets the user name and domain from the an email address using substring, strpos, and lengthfunctions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
SELECT
SUBSTRING (
'[email protected]',
1,
strpos(
'[email protected]',
'@'
) - 1
) AS user_name,
SUBSTRING (
'[email protected]',
strpos(
'[email protected]',
'@'
) + 1,
LENGTH (
'[email protected]'
)
) AS domain_name;

postgresql length function with substring function

Besides the length function, PostgreSQL provides the char_length and character_length functions that provide the same functionality.

Measure strings in bytes and bits

To get the number of bytes in a string, you use the octet_length function as follows:

1
OCTET_LENGTH(string);

See the following example:

1
2
SELECT
OCTET_LENGTH ('A'); -- 1 byte

It returns 1 byte.

1
2
SELECT
OCTET_LENGTH ('€'); -- 3 bytes

It returns 3 bytes. However, with the length function, it returns just 1.

1
2
SELECT
LENGTH ('€'); -- 1

To measure the number of bits of a string, you use the bit_length function as follows:

1
BIT_LENGTH(string);

See the following examples of using the bit_length function.

1
2
SELECT
BIT_LENGTH ('A'); -- 8 bits

1
2
SELECT
BIT_LENGTH ('€'); -- 24 bits

In this tutorial, we have shown you how to use length, bit_length, and octet_length functions to return the number of characters, the number of bits, and the number of bytes of a string.

Previous Tutorial: PostgreSQL Substring
Next Tutorial: PostgreSQL TRIM Function

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Aggregate Functions

  • PostgreSQL COUNT Function
  • PostgreSQL AVG Function
  • PostgreSQL MAX Function
  • PostgreSQL MIN Function
  • PostgreSQL SUM Function

PostgreSQL Conditional Expressions

  • PostgreSQL CASE
  • PostgreSQL NULLIF
  • PostgreSQL COALESCE

PostgreSQL String functions

  • PostgreSQL CONCAT Function
  • PostgreSQL TRIM Function
  • PostgreSQL LENGTH Function
  • PostgreSQL REPLACE
  • PostgreSQL Substring
  • PostgreSQL Letter Case Functions

PostgreSQL Operators

  • PostgreSQL CAST

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • A Look At Various PostgreSQL Integer Data Types
  • PostgreSQL Recursive View
  • Learn PostgreSQL Recursive Query By Example
  • Creating Updatable Views Using the WITH CHECK OPTION Clause
  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
  • How to Generate a Random Number in A Range
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Boolean Data Type with Practical Examples

More Tutorials

  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Functions
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.