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. colums to single row

colums to single row

Scheduled Pinned Locked Moved Database
comhelp
4 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.
  • R Offline
    R Offline
    Ramkumar_S
    wrote on last edited by
    #1

    Hi All, table ----- NAme Age Total ---- --- ------ Ram 26 800 kumar 36 300 Sam 34 200 output should be ------ Name:Ram Age:26 Total:800 ;Name:Kumar Age:36 Total:300;Name..... Please help me with this .tried using COALESCE but I could not achieve..

    Ramkumar ("When you build bridges you can keep crossing them. ") http://ramkumarishere.blogspot.com

    _ S G 3 Replies Last reply
    0
    • R Ramkumar_S

      Hi All, table ----- NAme Age Total ---- --- ------ Ram 26 800 kumar 36 300 Sam 34 200 output should be ------ Name:Ram Age:26 Total:800 ;Name:Kumar Age:36 Total:300;Name..... Please help me with this .tried using COALESCE but I could not achieve..

      Ramkumar ("When you build bridges you can keep crossing them. ") http://ramkumarishere.blogspot.com

      _ Offline
      _ Offline
      _Damian S_
      wrote on last edited by
      #2

      You can do it like this...

      DECLARE @String VARCHAR(8000)
      set @String = ''

      SELECT @String = @String + 'Name:' + ISNULL(Name,'NULL') + ' Age:' + Age + ' Total:' + Total + '; '
      FROM TABLE

      print @String

      Silence is golden... but duct tape is silver!! Booger Mobile - My bright green 1964 Ford Falcon - check out the blog here!! | If you feel generous - make a donation to Camp Quality!!

      1 Reply Last reply
      0
      • R Ramkumar_S

        Hi All, table ----- NAme Age Total ---- --- ------ Ram 26 800 kumar 36 300 Sam 34 200 output should be ------ Name:Ram Age:26 Total:800 ;Name:Kumar Age:36 Total:300;Name..... Please help me with this .tried using COALESCE but I could not achieve..

        Ramkumar ("When you build bridges you can keep crossing them. ") http://ramkumarishere.blogspot.com

        S Offline
        S Offline
        SilimSayo
        wrote on last edited by
        #3

        Why would you want the data in that format? If you happen to have 1000 records, would want all that data on one line?

        1 Reply Last reply
        0
        • R Ramkumar_S

          Hi All, table ----- NAme Age Total ---- --- ------ Ram 26 800 kumar 36 300 Sam 34 200 output should be ------ Name:Ram Age:26 Total:800 ;Name:Kumar Age:36 Total:300;Name..... Please help me with this .tried using COALESCE but I could not achieve..

          Ramkumar ("When you build bridges you can keep crossing them. ") http://ramkumarishere.blogspot.com

          G Offline
          G Offline
          gvprabu
          wrote on last edited by
          #4

          Hi.... Try This....

          DECLARE @Temp TABLE(Name VARCHAR(20),Age TINYINT, Total INT)

          INSERT INTO @Temp(Name,Age,Total)
          SELECT 'Ram',26,800
          UNION ALL
          SELECT 'kumar',36,300
          UNION ALL
          SELECT 'Sam',34,200

          DECLARE @SQLStr VARCHAR(8000)
          SET @SQLStr = ''

          SELECT @SQLStr = @SQLStr + 'Name:' + Name + ' Age:' + CAST(Age AS VARCHAR) + ' Total:' + CAST(Total AS VARCHAR) + '; '
          FROM @Temp

          PRINT @SQLStr

          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