Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. how to get column wise data in a row in sql ?

how to get column wise data in a row in sql ?

Scheduled Pinned Locked Moved Database
databasetutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ahmedalisha
    wrote on last edited by
    #1

    Hi Friends, i have data like this....... number -------------- 123 234 3435 5656 i want to display column wise data in one row like this... number --------------- 123,234,3435,5656 kindly tell me the solution for this? urgent:confused:

    C S M 3 Replies Last reply
    0
    • A ahmedalisha

      Hi Friends, i have data like this....... number -------------- 123 234 3435 5656 i want to display column wise data in one row like this... number --------------- 123,234,3435,5656 kindly tell me the solution for this? urgent:confused:

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      allisha wrote:

      i want to display column wise data in one row like this... number --------------- 123,234,3435,5656

      Read the data back and format a string appropiately. e.g.

      bool isFirst = true;
      StringBuilder sb = new StringBuilder();
      SqlDataReader reader = myCommand.ExecuteReader();
      while(reader.Read())
      {
      if (isFirst)
      isFirst = false;
      else
      sb.Append(",");
      sb.Append(reader.GetInt(0).ToString());
      }
      string displayResult = sb.ToString();


      Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

      A 1 Reply Last reply
      0
      • C Colin Angus Mackay

        allisha wrote:

        i want to display column wise data in one row like this... number --------------- 123,234,3435,5656

        Read the data back and format a string appropiately. e.g.

        bool isFirst = true;
        StringBuilder sb = new StringBuilder();
        SqlDataReader reader = myCommand.ExecuteReader();
        while(reader.Read())
        {
        if (isFirst)
        isFirst = false;
        else
        sb.Append(",");
        sb.Append(reader.GetInt(0).ToString());
        }
        string displayResult = sb.ToString();


        Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

        A Offline
        A Offline
        ahmedalisha
        wrote on last edited by
        #3

        hi Colin, first of all thanq but i want to show it in sql. if possible cau u tell me the sol in sql.:confused:

        1 Reply Last reply
        0
        • A ahmedalisha

          Hi Friends, i have data like this....... number -------------- 123 234 3435 5656 i want to display column wise data in one row like this... number --------------- 123,234,3435,5656 kindly tell me the solution for this? urgent:confused:

          S Offline
          S Offline
          Shetty_80
          wrote on last edited by
          #4

          I think through CURSOR only we can solve this issue. Try the below query.. You can also create one function with this query and get the value back in a single column. DECLARE @OUTPUT NVARCHAR(100), @NAME NVARCHAR(100) DECLARE LAST_CURSOR CURSOR LOCAL SCROLL STATIC FOR ---Here give your select query... SELECT NAME FROM DBT_EMPL_DTL OPEN LAST_CURSOR set @OUTPUT = '' FETCH NEXT FROM LAST_CURSOR INTO @NAME WHILE @@FETCH_STATUS = 0 BEGIN BEGIN set @OUTPUT = @OUTPUT + @NAME +',' END FETCH NEXT FROM LAST_CURSOR INTO @NAME END SET @OUTPUT = LTRIM(@OUTPUT) PRINT @OUTPUT CLOSE LAST_CURSOR DEALLOCATE LAST_CURSOR Cheers,

          Shetty

          1 Reply Last reply
          0
          • A ahmedalisha

            Hi Friends, i have data like this....... number -------------- 123 234 3435 5656 i want to display column wise data in one row like this... number --------------- 123,234,3435,5656 kindly tell me the solution for this? urgent:confused:

            M Offline
            M Offline
            M H 1 2 3
            wrote on last edited by
            #5

            if you are using sql server 2005 you can use the pivot and unpivot commands in your sql code to go from rows to columns or vice versa. It's pretty easy to use.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups