Niks
Niks

Reputation: 1007

How to convert comma sepearted string value to display as a column name

In PostgreSQL, I have generated Comma separated string of required columns dynamically. But how to convert them as Column name ?

I have tried it in SQL Server.. got success but want to do in postgreSQL

My Comma Separated String as : String_agg

7_headid,7_grade,8_headid,8_grade,6_headid,6_grade,5_headid,5_grade,4_headid,4_grade,3_headid,3_grade,15_headid,15_grade,9_headid,9_grade,1_headid,1_grade,2_headid,2_grade 

Required Output as :

Column name -->    Sr.No || 7_headid || 7_grade || 8_headid || 8_grade || 6_headid ||6_grade || So on
-----------------------------------------------------------------------  
Rows value  -->      1         2          3           4            5         6         7

Upvotes: 0

Views: 63

Answers (1)

Erwin Brandstetter
Erwin Brandstetter

Reputation: 656291

SELECT 1 AS "Str. No"
     , 2 AS "7_headid"
     , 3 AS "7_grade"
     ...

You have to double-quote identifiers like these violating basic rules.

Upvotes: 1

Related Questions