How to add single quotes around a list of columns names? - postgresql

I have a table which has few columns. I got the name of columns by getting it as an array_agg and then array_to_string. Like below:
"hospitalaccountrecord,locationname,patientkey,inpatientadmitdatetime,readmission,no_null_days_btw_admissions,cohort_assignment,admit_mon_feb,admit_mon_mar,admit_mon_apr,admit_mon_may,admit_mon_june,admit_mon_july,admit_mon_aug,admit_mon_sep,admit_mon_oct,a (...)"
The code I used was this:
select array_to_string(array_agg(column_name::text), ',')
from
(
select column_name
from
information_schema.columns
where table_schema='a' and
table_name = 'b'
order by columns.ordinal_position
) as v;
What I am looking for is the same thing but each column name should be enclosed in ''. Like
'hospitalaccountrecord','locationname','patientkey'
and so on.

Don't need two SELECTs, only
select string_agg(''''|| your_column_name ||'''', ',')
should do the job.

select string_agg(quoted_column_name, ',')
from
(
select '''' || column_name || '''' as quoted_column_name
from
information_schema.columns
where table_schema='a' and
table_name = 'b'
order by columns.ordinal_position
) as v;

Related

T-SQL: Get a tab-separated list of column names in a table - What's wrong with my example?

This SQL returns a comma-separated list of column names for the table 'MyTable'
DECLARE #colnames VARCHAR(max);
SELECT
#colnames = COALESCE(#colnames + ',', '') + column_name
FROM
CORP_MLR_Rebates.INFORMATION_SCHEMA.COLUMNS
WHERE
table_name = 'MyTable';
SELECT #colnames;
Why doesn't the following give me a tab-separated list the same columns? Instead, it is space-separated.
DECLARE #colnames VARCHAR(max);
SELECT
#colnames = COALESCE(#colnames + char(9), '') + column_name
FROM
CORP_MLR_Rebates.INFORMATION_SCHEMA.COLUMNS
WHERE
table_name = 'MyTable';
SELECT #colnames;
In my testing it does work, I guess it just depends on how you retrieve the results (I have to output the result to a file to get the correct results)
You will likely get spaces using the SSMS GUI. Returning the results as text or to a file will give you tabs; e.g. CHAR(9). Consider these three queries that all do the job:
;--== 1. Updatable Variable
DECLARE #colnames VARCHAR(max);
SELECT #colnames = COALESCE(#colnames + CHAR(9), '') + column_name
FROM INFORMATION_SCHEMA.COLUMNS AS c;
SELECT ColNames = #colnames;
;--== 2. Using STRING_AGG
SELECT ColNames = STRING_AGG(c.COLUMN_NAME , CHAR(9))
FROM INFORMATION_SCHEMA.COLUMNS AS c;
;--== 3. Using FOR XML PATH
SELECT ColNames =
(SELECT c.COLUMN_NAME+CHAR(9) FROM INFORMATION_SCHEMA.COLUMNS AS c FOR XML PATH(''));
Results:
If we switch to Results as Text:
Now we get (note the tabs):

Is it posible to use queried column_name in INSERT INTO statement?

Query:
SELECT string_agg(column_name::text, ',')
FROM information_schema.columns
WHERE table_name = 'my_table' and column_name !='id'
returns list of columns in my_table except 'id':
date_of_birth,first_name,last_name,e_mail
I would like to use this list in INSERT INTO statement when defining columns names. How should I do it, because following don't work.
INSERT INTO my_table2 (
SELECT string_agg(column_name::text, ',')
FROM information_schema.columns
WHERE table_name = 'my_table' and column_name !='id'
)
VALUES ('a', 'b', ...);
I have changing column count and order in my_table2 and my_table so I must assign names automatically .
You have to use dynamic SQL, that is, construct a second query from the result of the first one. There is no way to do it in a single statement.
With psql's \gexec, you could use
SELECT format($$INSERT INTO my_table2 (%s) VALUES ('a', 'b', ...)$$,
string_agg(quote_ident(column_name), ', ')
)
FROM information_schema.columns
WHERE table_name = 'my_table'
AND column_name <> 'id' \gexec

How to use string aggregation on concatenated result subquery in Postgres?

I am trying to use string aggregation on the results of a subquery that contains concatenated results. I would like to create a long text string containing the qualified name "schema.table" for a DB schema I have in Postgres. Do you know how to achieve it? Below is the code I have come up with (not working), it says
more than one row returned by a subquery used as an expression.
SELECT string_agg(
(SELECT CONCAT(table_schema, '.', table_name) table_schema_name
FROM information_schema.tables
WHERE table_schema = 'name_of_schema'), ' ')
FROM information_schema.tables
WHERE table_schema = 'name_of_schema'
what I have now is:
| table_schema_name |
name_of_schema.table1
name_of_schema.table2
name_of_schema.table3
What I would like to achieve is:
| agrreagated field |
name_of_schema.table1 name_of_schema.table2 name_of_schema.table3
This query should do the task you're asking for:
SELECT string_agg(CONCAT(table_schema, '.', table_name), ' ')
FROM information_schema.tables
WHERE table_schema = 'schema_name'
You should either use the query as table expression in your outer select:
SELECT string_agg(s.table_schema_name, ' ') FROM (
SELECT CONCAT(table_schema, '.', table_name) AS table_schema_name
FROM information_schema.tables
WHERE table_schema = 'public'
) s
or simply avoid the sub-select:
SELECT string_agg(CONCAT(table_schema, '.', table_name), ' ')
FROM information_schema.tables
WHERE table_schema = 'public';

postgresql - customized output file with comma separated columns

We are using postgresql 8/psql version 8.4. I am querying information_schema.columns for the column names and would like to generate a text file/output file so:
UNLOAD ('select
col1,
col2,
col3,
col4,
col5)
to more text here
or
UNLOAD ( 'select col1,col2,col3,col4,col5) to more text here
So, I'm basically looking to output the colname followed by a "," - colname,
Is this possible? Thanks for the help.
This will create a string like that:
SELECT 'UNLOAD ( ''select ' ||
array_to_string(array_agg(column_name::text), ',') ||
' to more text here'
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'whatever'
;
You can use \o to send it to a file, or use COPY (...) TO STDOUT.

SELECT ALL column_names in postgresql

I'm using PostgreSQL and I want to create a query that will display all column_names in a specific table.
Schema: codes
Table Name: watch_list
Here are the column_names in my table:
watch_list_id, watch_name, watch_description
I tried what I found in the web:
SELECT *
FROM information_schema.columns
WHERE table_schema = 'codes'
AND table_name = 'watch_list'
It output is not what I wanted. It should be:
watch_list_id, watch_name, watch_description
How to do this?
If you want all column names in a single row, you need to aggregate those names:
SELECT table_name, string_agg(column_name, ', ' order by ordinal_position) as columns
FROM information_schema.columns
WHERE table_schema = 'codes'
AND table_name = 'watch_list'
GROUP BY table_name;
If you remove the condition on the table name, you get this for all tables in that schema.
SELECT table_name FROM information_schema.tables WHERE table_schema='public'