0% found this document useful (0 votes)
231 views33 pages

Parsing and Manipulating Text PDF

The document discusses various string manipulation functions in PostgreSQL for reformatting, parsing, and manipulating string data. It provides examples of concatenating, changing case, replacing characters, determining string length, finding character positions, and extracting substrings. Functions demonstrated include concatenation operators, UPPER, LOWER, INITCAP, REPLACE, CHAR_LENGTH, POSITION, LEFT, RIGHT, SUBSTRING.

Uploaded by

dieko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
231 views33 pages

Parsing and Manipulating Text PDF

The document discusses various string manipulation functions in PostgreSQL for reformatting, parsing, and manipulating string data. It provides examples of concatenating, changing case, replacing characters, determining string length, finding character positions, and extracting substrings. Functions demonstrated include concatenation operators, UPPER, LOWER, INITCAP, REPLACE, CHAR_LENGTH, POSITION, LEFT, RIGHT, SUBSTRING.

Uploaded by

dieko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Reformatting string

and character data


F U N C T I O N S F O R M A N I P U L AT I N G D ATA I N P O S TG R E S Q L

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!
F U N C T I O N S F O R M A N I P U L AT I N G D ATA I N P O S TG R E S Q L
Parsing string and
character data
F U N C T I O N S F O R M A N I P U L AT I N G D ATA I N P O S TG R E S Q L

Brian Piccolo
Sr. Director, Digital Strategy
Determining the length of a string
SELECT
title,
CHAR_LENGTH(title)
FROM film;

+-------------------+---------------------+
| title | CHAR_LENGTH(title) |
|-------------------+---------------------|
| ACADEMY DINOSAUR | 16 |
| ACE GOLDFINGER | 14 |
| ADAPTATION HOLES | 16 |
+-------------------+---------------------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Determining the length of a string
SELECT
title,
LENGTH(title)
FROM film;

+-------------------+----------------+
| title | LENGTH(title) |
|-------------------+----------------|
| ACADEMY DINOSAUR | 16 |
| ACE GOLDFINGER | 14 |
| ADAPTATION HOLES | 16 |
+-------------------+----------------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Finding the position of a character in a string
SELECT
email,
POSITION('@' IN email)
FROM customer;

+-------------------------------------+------------------------+
| email | POSITION('@' IN email) |
|-------------------------------------|------------------------|
| [email protected] | 11 |
| [email protected] | 17 |
| [email protected] | 15 |
+-------------------------------------+------------------------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Finding the position of a character in a string
SELECT
email,
STRPOS(email, '@')
FROM customer;

+-------------------------------------+--------------------+
| email | STRPOS(email, '@') |
|-------------------------------------|--------------------|
| [email protected] | 11 |
| [email protected] | 17 |
| [email protected] | 15 |
+-------------------------------------+--------------------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Parsing string data
SELECT
LEFT(description, 50) extract first 50 chars

FROM film;

+----------------------------------------------------+
| description |
|----------------------------------------------------|
| A Epic Drama of a Feminist And a Mad Scientist who |
| A Astounding Epistle of a Database Administrator A |
| A Astounding Reflection of a Lumberjack And a Car |
+----------------------------------------------------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Parsing string data
SELECT
RIGHT(description, 50)
FROM film;

+----------------------------------------------------+
| description |
|----------------------------------------------------|
| who must Battle a Teacher in The Canadian Rockies |
| nd a Explorer who must Find a Car in Ancient China |
| Car who must Sink a Lumberjack in A Baloon Factory |
+----------------------------------------------------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Extracting substrings of character data
SELECT start position

SUBSTRING(description, 10, 50)


num of chars to extract
FROM
film AS f;

+----------------------------------------------------+
| description |
|----------------------------------------------------|
| ama of a Feminist And a Mad Scientist who must Bat |
| ing Epistle of a Database Administrator And a Expl |
| ing Reflection of a Lumberjack And a Car who must |
+----------------------------------------------------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Extracting substrings of character data
SELECT
SUBSTRING(email FROM 0 FOR POSITION('@' IN email))
FROM
customer;

+----------------------------------------------------+
| SUBSTRING(email FROM 0 FOR POSITION('@' IN email)) |
|----------------------------------------------------|
| MARY.SMITH |
| PATRICIA.JOHNSON |
| LINDA.WILLIAMS |
+----------------------------------------------------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Extracting substrings of character data
SELECT
SUBSTRING(email FROM POSITION('@' IN email)+1 FOR CHAR_LENGTH(email))
FROM
customer;

+-----------------------------------------------------------------------+
| SUBSTRING(email FROM POSITION('@' IN email)+1 FOR CHAR_LENGTH(email)) |
|-----------------------------------------------------------------------|
| sakilacustomer.org |
| sakilacustomer.org |
| sakilacustomer.org |
+-----------------------------------------------------------------------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Extracting substrings of character data
SELECT
SUBSTR(description, 10, 50)
analogous but more limited

FROM
film AS f;

+----------------------------------------------------+
| description |
|----------------------------------------------------|
| ama of a Feminist And a Mad Scientist who must Bat |
| ing Epistle of a Database Administrator And a Expl |
| ing Reflection of a Lumberjack And a Car who must |
+----------------------------------------------------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


SELECT
-- Extract the characters to the left of the '@'
LEFT(email, POSITION('@' IN email)-1) AS username,

-- Extract the characters to the right of the '@'


SUBSTRING(email FROM POSITION('@' IN email)+1 FOR LENGTH(email)) AS domain
FROM customer;

Let's practice!
F U N C T I O N S F O R M A N I P U L AT I N G D ATA I N P O S TG R E S Q L
Truncating and
padding string data
F U N C T I O N S F O R M A N I P U L AT I N G D ATA I N P O S TG R E S Q L

Brian Piccolo
Sr. Director, Digital Strategy
Removing whitespace from strings
TRIM([leading | trailing | both] [characters] from string)

First parameter: [leading | trailing | both] by deault both

Second parameter: [characters]

Third parameter: from string

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Removing whitespace from strings
SELECT TRIM(' padded ');

+--------+
| TRIM |
|--------|
| padded |
+--------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Removing whitespace from strings
SELECT LTRIM(' padded ');

+------------+
| LTRIM |
|------------|
| padded |
+------------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Removing whitespace from strings
SELECT RTRIM(' padded ');

+----------+
| RTRIM |
|----------|
| padded |
+----------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Padding strings with character data
SELECT LPAD('padded', 10, '#');

+-------------+
| LPAD |
|-------------|
| ####padded |
+-------------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Padding strings with whitespace
SELECT LPAD('padded', 10); SELECT LPAD('padded', 5);

+-------------+ +------------+
| LPAD | | LPAD |
|-------------| |------------|
| padded | | padde |
+-------------+ +------------+

when lenth àrameter is less than the original length of the str, the result
will be truncated

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


Padding strings with whitespace
SELECT RPAD('padded', 10, '#');

+-------------+
| RPAD |
|-------------|
| padded#### |
+-------------+

FUNCTIONS FOR MANIPULATING DATA IN POSTGRESQL


In this exercise, we are going to use the film and category tables to create a new field called
film_category by concatenating the category name with the film's title. You will also practice how to
truncate text fields like the film table's description column without cutting off a word.

To accomplish this we will use the REVERSE() function to help determine the position of the last
whitespace character in the description before we reach 50 characters. This technique can be used to
determine the position of the last character that you want to truncate and ensure that it is less than or
equal to 50 characters AND does not cut off a word.

SELECT
UPPER(c.name) || ': ' || f.title AS film_category,
-- Truncate the description without cutting off a word
Determine the position of the left(description, 50 -
last whitespace character of -- Subtract the position of the first whitespace character
the truncated description position(

Let's practice!
column and subtract it from ' ' IN REVERSE(LEFT(description, 50))
the number 50 as the second )
parameter in the first function )
above FROM
film AS f
INNER JOIN film_category AS fc
ON f.film_id = fc.film_id
F U N C T I O N S F O R M A N I P U L AT I N G D ATA I N P O S TG R E S Q L
INNER JOIN category AS c
ON fc.category_id = c.category_id;

You might also like