Re: Generating fields in views with search/replace?

From: Bosco Rama <postgres(at)boscorama(dot)com>
To: "Asfand Qazi (Sanger Institute)" <aq2(dot)sanger(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Generating fields in views with search/replace?
Date: 2011-05-05 19:37:19
Message-ID: 4DC2FC6F.9010709@boscorama.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Asfand Qazi (Sanger Institute) wrote:
>
> Say I have a table with fields 'template' and 'original_letter', and
> 'new_selected_letter'. 'template' could be 'abcdefg0abcdefg',
> original_letter could be 'A' and new_selected_letter could be 'B'.
>
> I want a view where I see 2 fields: 'original' as 'abcdefgAabcdefg'
> and 'new_selected' as 'abcdefgBabcdefg', where the view has replaced
> the '0' with original_letter or new_selected_letter respectively.

Well, in 8.4.7 you'd use something like:

create view xyz as
select regexp_replace(template, '0', original_letter) as original,
regexp_replace(template, '0', new_selected_letter) as new_selected
from template_table;

Should be the same in 9.x.

(See docs for more info on the regexp_replace() function)

HTH.

Later,
Bosco.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Johnston 2011-05-05 19:44:18 Re: Multiple table relationship constraints
Previous Message Rick Chu 2011-05-05 19:36:39 Re: psql tunneling thru a middle server