Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Use substring() in PostgreSQL
The substring()
function in PostgreSQL is used to extract a portion of a string based on a pattern or a specified length. This function is particularly useful when you need to manipulate string data within your database.
Syntax
substring(string FROM start FOR length)
The substring()
function works in the following way:
- string: The string you want to extract the substring from.
- start: The position in the string where the extraction begins.
- length (optional): The number of characters to extract starting from the
start
position. If omitted, it extracts until the end of the string.
Examples
Basic Example
SELECT substring('PostgreSQL' FROM 1 FOR 4); -- Output: Post
This example extracts the first 4 characters of the string 'PostgreSQL'
starting from position 1.
Extracting with Pattern Matching
SELECT substring('John Doe' FROM '^[a-zA-Z]+'); -- Output: John
In this example, we use a regular expression pattern to extract the first word in a string. The pattern '^[a-zA-Z]+'
matches the first sequence of alphabetic characters.
Using substring()
with a Table
SELECT substring(name FROM 1 FOR 3) AS short_name FROM users;
Here, the substring()
function is applied to the name
column in the users
table to extract the first 3 letters of each user's name.
Practical Applications
- Data Cleaning: You can use
substring()
to remove unwanted characters or extract specific parts of a string (like domain names or phone numbers). - Text Extraction: If you store structured data in a single column, you can use
substring()
to extract specific parts of that data for analysis.