Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jEOcg-00032V-Ba for pgsql-docs@arkaria.postgresql.org; Wed, 18 Mar 2020 02:39:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jEOce-0006JU-Bz for pgsql-docs@arkaria.postgresql.org; Wed, 18 Mar 2020 02:39:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jEOce-0006J6-46 for pgsql-docs@lists.postgresql.org; Wed, 18 Mar 2020 02:39:56 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jEOca-0004cM-DZ for pgsql-docs@lists.postgresql.org; Wed, 18 Mar 2020 02:39:55 +0000 Received: from bruce by momjian.us with local (Exim 4.92) (envelope-from ) id 1jEOcY-0000I9-Ue; Tue, 17 Mar 2020 22:39:50 -0400 Date: Tue, 17 Mar 2020 22:39:50 -0400 From: Bruce Momjian To: abdulrahim.s@gmail.com, pgsql-docs@lists.postgresql.org Subject: Re: REGEXP_REPLACE : How to use a column value in the regex Message-ID: <20200318023950.GF17915@momjian.us> References: <158192143115.6745.7931715775932027756@wrigleys.postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <158192143115.6745.7931715775932027756@wrigleys.postgresql.org> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, Feb 17, 2020 at 06:37:11AM +0000, PG Doc comments form wrote: > The following documentation comment has been logged on the website: > > Page: https://www.postgresql.org/docs/12/functions-matching.html > Description: > > The documentation doesn't talk about any regex syntax for using a column > from the db in the pattern. There is no mention if this is NOT supported. > All the examples are using static string and none using a DB column > values. > e.g. I have a field *portfolio* which had data like CloudPlatform (Public), > CloudPlatform (Private), MobilePlatfom (Public),.. etc > These values are coming from another table *platformtypes" which has these > values Public, Private, Hybrid...etc. > I want to replace these types from the portfolio names, i was able to do it > using string > *SELECT > Portfolios.PortfolioName,REGEXP_REPLACE(Portfolios.PortfolioName,'(\(Public\))',''), > but I would like to use something like > *REGEXP_REPLACE(Portfolios.PortfolioName,'(\(platformtypes.Name\)),'')* Sure, you can do it ('a' -> 'X'): CREATE TABLE TEST (x text); INSERT INTO test VALUES ('a'); SELECT REGEXP_REPLACE(relname, x, 'X') FROM pg_class, test; regexp_replace ----------------------------------------------- pg_toXst_36526 pg_toXst_36526_index test pg_stXtistic pg_type pg_toXst_2600 You can also combine column names with literal text ('ta' -> 'YY'): SELECT REGEXP_REPLACE(relname, 't' || x, 'YY') FROM pg_class, test; regexp_replace ----------------------------------------------- pg_toast_36526 pg_toast_36526_index test pg_sYYtistic I didn't think it was worth documenting this. -- Bruce Momjian https://momjian.us EnterpriseDB https://enterprisedb.com + As you are, so once was I. As I am, so you will be. + + Ancient Roman grave inscription +