6

I want to retrieve some part of given string.

Here is the following example for the string:

Example: In SQL Server

Declare @Names varchar = 'H1,H2,H3,'

SELECT STUFF(@Names,1,CHARINDEX(',',@Names,0),'');

After referring this : 'stuff' and 'for xml path('')' from SQL Server in Postgresql.

String_agg can't help me for this scenario.

Community
  • 1
  • 1
MAK
  • 6,824
  • 25
  • 74
  • 131
  • So what does `stuff()` do? –  Mar 10 '15 at 07:25
  • @a_horse_with_no_name, It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position. Syntax : `STUFF ( character_expression , start , length , replaceWith_expression )`. – MAK Mar 10 '15 at 07:31
  • 2
    Can you show an example of output? Or describe what you want to achieve. My guess - `regexp_replace()` would do the trick but i do not understand what you want to do. – Ihor Romanchenko Mar 10 '15 at 09:23

1 Answers1

8

You are looking for equivalent TSQL function of STUFF in PostgrSQL which i think is: overlay

so for example:

SELECT STUFF('abcdef', 2, 3, 'ijklmn');

and

select overlay('abcdef' placing 'ijklmn' from 2 for 3)

give both the same result which is;

'aijklmnef'

in other words:

They inserts a string into another string. It deletes a specified length of characters in the 
first string at the start position and then inserts the second string into the first string at 
the start position.
Houari
  • 5,326
  • 3
  • 31
  • 54