Functions Related to Strings and Text

PostgreSQL Functions

The Ultimate PostGIS course

Get full access to this course and all its content.

₹3399.00 ₹1299.00
Buy Course Now
Chapter Info
Course The Ultimate PostGIS course
Module PostgreSQL Functions
Chapter Functions Related to Strings and Text

Chapter Content

String manipulation is a fundamental skill in database management. PostgreSQL provides a rich set of built-in functions to clean, transform, and analyze text data effectively. Whether you're working with user data, GIS applications, or data analytics, mastering these functions will significantly improve your data processing workflows.

Sample Data Setup

Let's start with a practical example using a users table that contains various text fields:

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  full_name TEXT,
  email TEXT,
  bio TEXT
);

INSERT INTO users (full_name, email, bio) VALUES
('Alice Smith', 'alice@example.COM', 'GIS professional from New York.'),
('bob jones', 'BOB.JONES@Gmail.com', 'data analyst'),
('CHARLIE brown', 'charlie@sample.org', 'Researcher | Analyst');

Case Conversion Functions

Text data often comes in inconsistent formats. PostgreSQL provides three essential case conversion functions:

SELECT
  full_name,
  UPPER(full_name) AS upper_name,
  LOWER(full_name) AS lower_name,
  INITCAP(full_name) AS proper_case
FROM users;
Function Purpose Example
UPPER() Converts to uppercase ALICE SMITH
LOWER() Converts to lowercase alice smith
INITCAP() Capitalizes first letter of each word Alice Smith

Trimming and Padding

Handle unwanted whitespace and format strings for consistent display:

SELECT
  '   GIS Rocks   ' AS original,
  TRIM('   GIS Rocks   ') AS trimmed,
  LTRIM('   GIS Rocks   ') AS left_trim,
  RTRIM('   GIS Rocks   ') AS right_trim,
  LPAD('123', 5, '0') AS left_padded,
  RPAD('abc', 6, '.') AS right_padded;
Function Purpose Result
TRIM() Removes spaces from both ends GIS Rocks
LTRIM() Removes left spaces GIS Rocks
RTRIM() Removes right spaces GIS Rocks
LPAD() Pads left with specified character 00123
RPAD() Pads right with specified character abc...

String Concatenation

Combine multiple text fields for reports and formatted output:

SELECT
  full_name || ' <' || LOWER(email) || '>' AS formatted_email
FROM users;

SELECT CONCAT_WS(' - ', full_name, email) AS combined FROM users;

Finding and Replacing Text

Clean and standardize your data by locating and replacing specific text patterns:

SELECT
  bio,
  POSITION('analyst' IN LOWER(bio)) AS position_index,
  REPLACE(LOWER(bio), 'analyst', 'specialist') AS updated_bio
FROM users;
Function Purpose Example
POSITION() Finds position of substring Returns 6 for 'analyst' in 'data analyst'
REPLACE() Replaces all occurrences Changes 'analyst' to 'specialist'

Substring and Length Operations

Extract portions of strings and measure their length:

SELECT
  full_name,
  SUBSTRING(full_name FROM 1 FOR 5) AS first_five,
  LENGTH(full_name) AS name_length
FROM users;

String Splitting and Joining

Break strings into components and reassemble them:

SELECT
  full_name,
  regexp_split_to_array(full_name, ' ') AS name_parts
FROM users;

SELECT
  array_to_string(regexp_split_to_array(full_name, ' '), '-') AS hyphenated
FROM users;

Pattern Matching

Perform flexible searches with case-insensitive matching:

SELECT full_name FROM users WHERE email ILIKE '%gmail.com';

Real-World GIS Example

In GIS workflows, layer names often need cleaning for proper display:

CREATE TABLE layers (
  id SERIAL PRIMARY KEY,
  raw_name TEXT
);

INSERT INTO layers (raw_name) VALUES
('  water_body '),
('forest_area'),
('urban-zone');

SELECT
  id,
  TRIM(raw_name) AS trimmed,
  REPLACE(raw_name, '_', ' ') AS readable,
  INITCAP(REPLACE(raw_name, '_', ' ')) AS proper_label
FROM layers;

This transforms raw layer names like ' water_body ' into clean, readable labels like 'Water Body'.

Best Practices

  1. Normalize data during import: Clean text fields consistently when loading data
  2. Store emails in lowercase: Makes searching and matching more reliable
  3. Handle NULLs gracefully: Always consider NULL values in string operations
  4. Use built-in functions: They're optimized and more reliable than custom solutions
  5. Chain functions: Combine multiple operations for complex transformations

Practice Exercises

Try these exercises with the sample data:

  1. Create a formatted display name: "SMITH, Alice (alice@example.com)"
  2. Count the number of words in each bio
  3. Extract the domain from each email address
  4. Replace all punctuation in bios with spaces

Summary

PostgreSQL's string functions provide powerful tools for text manipulation. From basic case conversion to complex pattern matching, these functions enable you to clean, transform, and analyze text data efficiently. Master these fundamentals, and you'll be well-equipped to handle any text processing challenge in your database workflows.

Whether you're building reports, cleaning imported data, or preparing text for display in applications, these string functions form the foundation of effective data management in PostgreSQL.