Row into Columns

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • baburk
    New Member
    • Oct 2006
    • 111

    Row into Columns

    My table Structure:

    Col1 Col2
    1 Address
    2 Address2
    3 city
    4 name




    I want to Select from this and display it like
    Name Address Adress2 city name
    arun 34kkk kkk XXX
    babu YY YY YY
  • vksingh24
    New Member
    • Dec 2007
    • 21

    #2
    Originally posted by baburk
    My table Structure:

    Col1 Col2
    1 Address
    2 Address2
    3 city
    4 name




    I want to Select from this and display it like
    Name Address Adress2 city name
    arun 34kkk kkk XXX
    babu YY YY YY
    Try this:

    Code:
    select DISTINCT A.COL2 + ',' + B.COL2 + ',' + C.COL2 + ',' + D.COL2 AS 'Address' FROM EMP A,EMP B,EMP C,EMP D WHERE A.COL1=4 AND B.COL1=1 AND C.COL1=2 AND D.COL1=3

    Comment

    • deepuv04
      Recognized Expert New Member
      • Nov 2007
      • 227

      #3
      Originally posted by vksingh24
      Try this:

      Code:
      select DISTINCT A.COL2 + ',' + B.COL2 + ',' + C.COL2 + ',' + D.COL2 AS 'Address' FROM EMP A,EMP B,EMP C,EMP D WHERE A.COL1=4 AND B.COL1=1 AND C.COL1=2 AND D.COL1=3

      this query will be fine if you have only four records, But in the example given

      the output he want look like
      arun 34kkk kkk XXX
      babu YY YY YY

      how do you get the rest of the rocrds....

      thanks

      Comment

      • vksingh24
        New Member
        • Dec 2007
        • 21

        #4
        Originally posted by deepuv04
        this query will be fine if you have only four records, But in the example given

        the output he want look like
        arun 34kkk kkk XXX
        babu YY YY YY

        how do you get the rest of the rocrds....

        thanks


        If you have more records thanyou need to write stored procedure to get the output.

        Comment

        Working...