w3resource

PostgreSQL LENGTH() function


LENGTH() function

The PostgreSQL length() function is used to determine the number of characters in a given string. It is useful for evaluating string lengths in various contexts, such as data validation, formatting, and analysis.

Uses of LENGTH() Function

  • Determine String Length: Calculate the number of characters in a string.

  • Data Validation: Ensure that strings meet certain length requirements.

  • Filtering Data: Use in WHERE clauses to filter results based on string length.

  • Data Formatting: Format output based on string length for better readability.

  • Performance Optimization: Optimize queries and data storage by analyzing string lengths.

  • Combining with Other Functions: Use alongside other string functions for complex data manipulations.

Syntax:

length(<string>)

PostgreSQL Version: 9.3

Visual Presentation of PostgreSQL LENGTH() function

Pictorial presentation of PostgreSQL LENGTH() function


Example: PostgreSQL LENGTH() function:

In the example below the length function returns the length of the given string 'w3resource'.

SQL Code:

SELECT length('w3resource')
AS "Length of a String";

Output:

 Length of a String
--------------------
                 10
(1 row)

Example of PostgreSQL LENGTH() function using column :

Sample Table: employees.

 employee_id | first_name  |  last_name  |  email   |    phone_number    | hire_date  |   job_id   |  salary  | commission_pct | manager_id | department_id
-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------
         100 | Steven      | King        | SKING    | 515.123.4567       | 1987-06-17 | AD_PRES    | 24000.00 |           0.00 |          0 |            90
         101 | Neena       | Kochhar     | NKOCHHAR | 515.123.4568       | 1987-06-18 | AD_VP      | 17000.00 |           0.00 |        100 |            90
         102 | Lex         | De Haan     | LDEHAAN  | 515.123.4569       | 1987-06-19 | AD_VP      | 17000.00 |           0.00 |        100 |            90
         103 | Alexander   | Hunold      | AHUNOLD  | 590.423.4567       | 1987-06-20 | IT_PROG    |  9000.00 |           0.00 |        102 |            60
         104 | Bruce       | Ernst       | BERNST   | 590.423.4568       | 1987-06-21 | IT_PROG    |  6000.00 |           0.00 |        103 |            60
         105 | David       | Austin      | DAUSTIN  | 590.423.4569       | 1987-06-22 | IT_PROG    |  4800.00 |           0.00 |        103 |            60
         106 | Valli       | Pataballa   | VPATABAL | 590.423.4560       | 1987-06-23 | IT_PROG    |  4800.00 |           0.00 |        103 |            60
         107 | Diana       | Lorentz     | DLORENTZ | 590.423.5567       | 1987-06-24 | IT_PROG    |  4200.00 |           0.00 |        103 |            60
         108 | Nancy       | Greenberg   | NGREENBE | 515.124.4569       | 1987-06-25 | FI_MGR     | 12000.00 |           0.00 |        101 |           100
         109 | Daniel      | Faviet      | DFAVIET  | 515.124.4169       | 1987-06-26 | FI_ACCOUNT |  9000.00 |           0.00 |        108 |           100
		 ..................
         206 | William     | Gietz       | WGIETZ   | 515.123.8181       | 1987-10-01 | AC_ACCOUNT |  8300.00 |           0.00 |        205 |           110

View the table

The example below, returns the first_name and the length of first_name ( how many characters contain in the first name ) from the employees where the length of first_name is more than 7.

SQL Code:

SELECT first_name,length(first_name) 
AS "Length of a First Name" 
FROM employees 
WHERE length(first_name)>7;

Output:

 first_name  | Length of a First Name
-------------+------------------------
 Alexander   |                      9
 Jose Manuel |                     11
 Alexander   |                      9
 Christopher |                     11
 Danielle    |                      8
 Harrison    |                      8
 Elizabeth   |                      9
 Jonathon    |                      8
 Kimberely   |                      9
 Jennifer    |                      8
 Jennifer    |                      8
(11 rows)

PREV : LEFT function
NEXT : LPAD function



Follow us on Facebook and Twitter for latest update.