1

In SQL Server it's possible to do inline variable assignment.

For example, table dbo.tblSynonym:

+--+-------+-----------+
|id|keyword|replacement|
+--+-------+-----------+
|1 |aaa    |bbb        |
|2 |xxx    |yyy        |
|3 |ddd    |eee        |
+--+-------+-----------+

when I run this:

DECLARE @body varchar(max)='aaa111xxx111ddd' SELECT @body = REPLACE(@body,keyword,replacement) FROM dbo.tblSynonym SELECT @body

The result should be bbb111yyy111eee.

So the value of @body will be updated on each row (interaction) and the replace input will be from result of previous rows on the source table.

Is it possible to do something like this in postgres (without cursor)?

Thanks

NirKa
  • 697
  • 1
  • 7
  • 10
  • Please **[edit]** your question and add some [sample data](http://plaintexttools.github.io/plain-text-table/) and the expected output based on that data. [Formatted text](http://stackoverflow.com/help/formatting) please, [no screen shots](http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557). ([edit] your question - do **not** post code or additional information in comments) –  Jul 18 '18 at 09:54
  • Variables can only be used in PL/pgSQL. In "plain SQL" there are no variables. But even PL/pgSQL variables can't be used like that. –  Jul 18 '18 at 09:55
  • So looks like the only option is to create dynamic code with multiple "replace" according to number of rows in dbo.tblSynonym – NirKa Jul 18 '18 at 10:11
  • This is impossible to answer unless you show us some sample input data and the expected output (**[Edit]** your question - do **not** post code or additional information in comments) –  Jul 18 '18 at 10:13
  • @NirKa : have you find any answer to this problem ? – YogeshR Jun 11 '19 at 07:54

1 Answers1

0

I think this will help you

regexp_replace('Thomas', '.[mN]a.', 'M')  //ThM

Look here

https://www.postgresql.org/docs/current/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP

Sam lee
  • 13
  • 1
  • 5
  • I don't see how it helps, the replace needs to occur on the result of the previous replace. – NirKa Jul 18 '18 at 10:09