0
0
PostgreSQLquery~5 mins

Substring and overlay functions in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the substring function do in PostgreSQL?
The substring function extracts a part of a string based on position and length. For example, substring('hello' from 2 for 3) returns 'ell'.
Click to reveal answer
beginner
How do you use the overlay function in PostgreSQL?
The overlay function replaces a part of a string with another string. Syntax: overlay(original_string placing new_string from start_position for length). It returns the modified string.
Click to reveal answer
beginner
Example: What is the result of substring('abcdef' from 3 for 2)?
It returns 'cd' because it starts at position 3 and takes 2 characters.
Click to reveal answer
beginner
Example: What does overlay('abcdef' placing 'XY' from 2 for 3) return?
It returns 'aXYef'. It replaces 3 characters starting at position 2 with 'XY'.
Click to reveal answer
intermediate
Can substring use regular expressions in PostgreSQL?
Yes, substring can extract parts of a string using a regular expression pattern. For example, substring('abc123def' from '\d+') returns '123'.
Click to reveal answer
What does substring('hello world' from 7 for 5) return?
A'world'
B'hello'
C'world!'
D'hello world'
Which function replaces part of a string with another string in PostgreSQL?
Aoverlay
Bsubstring
Creplace
Dconcat
What is the result of overlay('abcdef' placing 'XY' from 3 for 2)?
A'XYcdef'
B'aXYdef'
C'abcXYf'
D'abXYef'
Can substring extract text using a regular expression?
AOnly in MySQL
BNo
CYes
DOnly with overlay
What happens if substring length is longer than the string?
AReturns an error
BReturns substring up to string end
CReturns empty string
DReturns full string twice
Explain how to extract a part of a string using the substring function in PostgreSQL.
Think about specifying where to start and how many characters to take.
You got /4 concepts.
    Describe how the overlay function works and give an example.
    It’s like cutting out a piece and putting a new piece in its place.
    You got /4 concepts.