Re: variable value in array_to_string

From: "Armand Pirvu (home)" <armand(dot)pirvu(at)gmail(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: variable value in array_to_string
Date: 2016-11-22 01:33:37
Message-ID: 5EB204CE-7B9A-40BD-9A18-68FA14A89F93@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

1 - I added some new notes.
2 - That code I know and works fine. I used it in slightly different contexts in other projects

Here it is a case .

A table with two columns , data type is irrelevant

I have a need in which I may get a.col1,a.col2 and other variations

Point is yes I could build any variation re-running a slightly different way the query in the catalogs

But why hit 3 or 4 times when I could manipulate an array like below ? This code just came in my mind (and works)

CREATE OR REPLACE FUNCTION test1 () RETURNS void AS $$
DECLARE
var1 text[];
var2 text;
var3 text;
var4 text;
begin
execute
'select ARRAY( select column_name::text from information_schema.columns where table_name='
||
quote_literal('foo')
||')' into var1;
raise notice '%', var1;
select array_to_string(array(select 'a.'||unnest(var1)),',') into var4;
raise notice '%', var4;
END;
$$ LANGUAGE plpgsql ;

levregdb1=# select test1(); NOTICE: {col1,col2}
NOTICE: a.col1,a.col2

Note the a. being prepended to each of the array’s elements

3 - I appreciate the help. Keep in mind I am not a developer and this is a new venture for me. On the same token I am not an ignorant either . What irritates me is the TRUE lack in this case of explanations in the docs. I know another DBMS just as bad in the documentation area (worse in fact)

I turned to the community. And yes I know how to make cases since I worked support.

Frankly I am turning off since I sure as heck don’t like being patronized. Did not accept this (and never will) from my own mom let alone somebody else

My apologies for asking help. Will try not to do it again

Thanks and laters

On Nov 21, 2016, at 7:12 PM, David G. Johnston <david(dot)g(dot)johnston(at)gmail(dot)com> wrote:

> Please don't top-post - it makes following the thread a lot harder.
>
> On Mon, Nov 21, 2016 at 4:15 PM, Armand Pirvu (home) <armand(dot)pirvu(at)gmail(dot)com> wrote:
> Played with unnest but not much luck
>
> If you want help you will need to show your work - ideally with examples that can execute with meaningful data on an empty database.
>
> The follow gives the expected results:
>
> DO $$
> DECLARE
> foo text[];
> foo1 text;
> delim text := ',';
> begin
> foo := ARRAY['one','two']::text[];
> foo1 := array_to_string(foo, delim);
> RAISE NOTICE '%', foo1;
> END;
> $$;
>
> NOTICE: one,two
>
> David J.
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2016-11-22 01:58:57 Re: variable value in array_to_string
Previous Message David G. Johnston 2016-11-22 01:12:22 Re: variable value in array_to_string