Transposing Data of Table as Columns in SQL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TimHop12
    New Member
    • Nov 2006
    • 24

    Transposing Data of Table as Columns in SQL

    Lets consider the following query:

    Code:
    Select Col1, Col2 from Table1 where Colx = 'A5100650867'
    This gives result as:
    Code:
    Col1                         Col2
    E-mail Address	            xyz@GMAIL.com
    Web Site                      www.GOOGLE.com
    I want the outcome as:

    Code:
    E-mail Address         Web Site
    xyz@GMAIL.com           www.GOOGLE.com
    So the new columns inthe resultset are 'E-mail Address' and 'Web Site'.

    [HTML]Note: This is just a simple example I have considered. Potentially there can be multiple things like that e.g. E-Mail, E-Mail2, Pager, Mobile etc etc...[/HTML]

    Please advice on this. Thank you.
  • dusana
    New Member
    • Oct 2007
    • 3

    #2
    Originally posted by TimHop12
    Lets consider the following query:

    Code:
    Select Col1, Col2 from Table1 where Colx = 'A5100650867'
    This gives result as:
    Code:
    Col1                         Col2
    E-mail Address	            xyz@GMAIL.com
    Web Site                      www.GOOGLE.com
    I want the outcome as:

    Code:
    E-mail Address         Web Site
    xyz@GMAIL.com           www.GOOGLE.com
    So the new columns inthe resultset are 'E-mail Address' and 'Web Site'.

    [HTML]Note: This is just a simple example I have considered. Potentially there can be multiple things like that e.g. E-Mail, E-Mail2, Pager, Mobile etc etc...[/HTML]

    Please advice on this. Thank you.


    try this

    Select ltrim(rtrim(Col 1)) as Col1, ltrim(rtrim(Col 2)) as Col2 from Table1 where Colx = 'A5100650867'

    Comment

    Working...