This is a place where you can find the diy stuffs that mom will love and technical stuffs, specially computer programming. Working full time as a software developer, I try to update myself with the new technologies. And, being a mother of the beautiful daughter, I love to entertain my kid with new things and play with her.

Thursday, 31 July 2014

Converting rows to column in the Oracle

While writing a SQL query, sometimes you often want to convert the rows to column. This will be hellpful when you are joining with the another table and you don't want the result to be dublicated per row.

Example:
Col_idCol_Name
1Apples
2Oranges
3Grapes
4Pineapple

SQL Query:
SELECT RTRIM(XMLAGG(XMLELEMENT(e,a.col_Name
    || '; ')).EXTRACT('//text()'),'; ') new_column
  FROM TABLE_NAME a;

Output:

New_column
Apples,Oranges,Grapes,Pineapple

0 comments:

Post a Comment