Reformatting string and character data

Functions for Manipulating Data in PostgreSQL

Brian Piccolo

Sr. Director, Digital Strategy

Topics

  • Reformatting string and character data.
  • Parsing string and character data.
  • Determine string length and character position.
  • Truncating and padding string data.
Functions for Manipulating Data in PostgreSQL

The string concatenation operator

SELECT 
  first_name,
  last_name,
  first_name || ' ' || last_name AS full_name
FROM customer
+------------+-----------+-------------------+
| first_name | last_name | full_name         |
|------------|-----------|-------------------|
| MARY       | SMITH     | MARY SMITH        |
| LINDA      | WILLIAMS  | LINDA WILLIAMS    |
+------------+-----------+-------------------+
Functions for Manipulating Data in PostgreSQL

String concatenation with functions

SELECT 
  CONCAT(first_name,' ', last_name) AS full_name
FROM customer;
+--------------------------------------------+
| first_name | last_name | full_name         |
|--------------------------------------------|
| MARY       | SMITH     | MARY SMITH        |
| LINDA      | WILLIAMS  | LINDA WILLIAMS    |
+--------------------------------------------+
Functions for Manipulating Data in PostgreSQL

String concatenation with a non-string input

SELECT 
  customer_id || ': ' 
  || first_name || ' ' 
  || last_name AS full_name
FROM customer;
+-------------------+
| full_name         |
|-------------------|
| 1: MARY SMITH     |
| 2: LINDA WILLIAMS |
+-------------------+
Functions for Manipulating Data in PostgreSQL

Changing the case of string

SELECT 
  UPPER(email) 
FROM customer;
+-------------------------------------+
| UPPER(email)                        |
|-------------------------------------|
| [email protected]       |
| [email protected] |
| [email protected]   |
+-------------------------------------+
Functions for Manipulating Data in PostgreSQL

Changing the case of string

SELECT 
  LOWER(title) 
FROM film;
+-------------------+
| LOWER(title)      |
|-------------------|
| academy dinosaur  | 
| ace goldfinger    |
| adaptation holes  |
+-------------------+
Functions for Manipulating Data in PostgreSQL

Changing the case of string

SELECT 
  INITCAP(title) 
FROM film;
+-------------------+
| INITCAP(title)    |
|-------------------|
| Academy Dinosaur  | 
| Ace Goldfinger    |
| Adaptation Holes  |
+-------------------+
Functions for Manipulating Data in PostgreSQL

Replacing characters in a string

SELECT description FROM film;
+---------------------------------------------------------+
| description                                             |
|---------------------------------------------------------|
| A Epic Drama of a Feminist And a Mad Scientist...       |
| A Astounding Epistle of a Database Administrator...     |
| A Astounding Reflection of a Lumberjack And a Car...    |
| A Fanciful Documentary of a Frisbee And a Lumberjack... |
| A Fast-Paced Documentary of a Pastry Chef And a...      |
+---------------------------------------------------------+
Functions for Manipulating Data in PostgreSQL

Replacing characters in a string

SELECT 
    REPLACE(description, 'A Astounding', 
              'An Astounding') as description 
FROM film;
+---------------------------------------------------------+
| description                                             |
|---------------------------------------------------------|
| A Epic Drama of a Feminist And a Mad Scientist...       |
| An Astounding Epistle of a Database Administrator...    |
| An Astounding Reflection of a Lumberjack And a Car...   |
+---------------------------------------------------------+
Functions for Manipulating Data in PostgreSQL

Manipulating string data with REVERSE

SELECT 
  title, 
  REVERSE(title) 
FROM 
  film AS f;
+-------------------------------------+
| title            | reverse(title)   |
|-------------------------------------|
| ACADEMY DINOSAUR | RUASONID YMEDACA |
| ACE GOLDFINGER   | REGNIFDLOG ECA   |
+-------------------------------------+
Functions for Manipulating Data in PostgreSQL

Let's practice!

Functions for Manipulating Data in PostgreSQL

Preparing Video For Download...