Skip to content

OVERLAY

OVERLAY(string PLACING new_substring FROM position [ FOR count ])

Section titled “OVERLAY(string PLACING new_substring FROM position [ FOR count ])”
  • string: This parameter specifies the original string where the replacement will take place.
  • placing new_substring: It indicates the substring which is to be placed in the original string.
  • from position: This parameter defines the starting point from where the replacement should be initiated in the original string.
  • for count: An optional argument that specifies the number of characters from the original string that should be replaced by the new_substring. If not specified, the length of new_substring is used.
SELECT OVERLAY('PostgreSQL is great' PLACING 'fantastic' FROM 16 FOR 5);
overlay
------------------
PostgreSQL is fantastic
(1 row)

The OVERLAY function in the above example replaces a substring from a string. The substring ‘great’ (which is 5 characters long and starts at the 16th position in the original string ‘PostgreSQL is great’) is replaced with ‘fantastic’. Hence, the output ‘PostgreSQL is fantastic’.