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. concatenate recordset into one row

concatenate recordset into one row

Scheduled Pinned Locked Moved Database
databasetoolsquestion
6 Posts 3 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.
  • J Offline
    J Offline
    JKroschel
    wrote on last edited by
    #1

    I am working on an asp script that pulls data from ms sql. In one part, it pulls the days of the week that an event occurs on. If I do something like this: select dayofweek from schedule where id=5 I get: --------- dayofweek --------- M --------- T --------- W --------- What I need is a sql statement that does something like this: --------- dayofweek --------- M,T,W --------- Is there a way to select them into one record like this even though they are stored in multiple rows in the table? Thanks, Jim

    A 1 Reply Last reply
    0
    • J JKroschel

      I am working on an asp script that pulls data from ms sql. In one part, it pulls the days of the week that an event occurs on. If I do something like this: select dayofweek from schedule where id=5 I get: --------- dayofweek --------- M --------- T --------- W --------- What I need is a sql statement that does something like this: --------- dayofweek --------- M,T,W --------- Is there a way to select them into one record like this even though they are stored in multiple rows in the table? Thanks, Jim

      A Offline
      A Offline
      airbus380
      wrote on last edited by
      #2

      Try this T SQL script (you ca make a SQL Server function) --parameter @id DECLARE @id INT SET @id = 5 DECLARE @ssum NVARCHAR(255) DECLARE @dayofweek NVARCHAR(25) SET @ssum = '' DECLARE crs CURSOR FOR SELECT DAYOFWEEK FROM schedule WHERE ID = @id OPEN crs FETCH NEXT FROM crs INTO @dayofweek WHILE( @@FETCH_STATUS = 0 ) BEGIN SET @ssum = @ssum + ',' + @dayofweek FETCH NEXT FROM crs INTO @dayofweek END CLOSE crs DEALLOCATE crs IF( LEN(@ssum) != 0 ) SET @ssum = SUBSTRING(@ssum,2,255) PRINT @ssum

      J L 2 Replies Last reply
      0
      • A airbus380

        Try this T SQL script (you ca make a SQL Server function) --parameter @id DECLARE @id INT SET @id = 5 DECLARE @ssum NVARCHAR(255) DECLARE @dayofweek NVARCHAR(25) SET @ssum = '' DECLARE crs CURSOR FOR SELECT DAYOFWEEK FROM schedule WHERE ID = @id OPEN crs FETCH NEXT FROM crs INTO @dayofweek WHILE( @@FETCH_STATUS = 0 ) BEGIN SET @ssum = @ssum + ',' + @dayofweek FETCH NEXT FROM crs INTO @dayofweek END CLOSE crs DEALLOCATE crs IF( LEN(@ssum) != 0 ) SET @ssum = SUBSTRING(@ssum,2,255) PRINT @ssum

        J Offline
        J Offline
        JKroschel
        wrote on last edited by
        #3

        Awesome. Thanks!

        1 Reply Last reply
        0
        • A airbus380

          Try this T SQL script (you ca make a SQL Server function) --parameter @id DECLARE @id INT SET @id = 5 DECLARE @ssum NVARCHAR(255) DECLARE @dayofweek NVARCHAR(25) SET @ssum = '' DECLARE crs CURSOR FOR SELECT DAYOFWEEK FROM schedule WHERE ID = @id OPEN crs FETCH NEXT FROM crs INTO @dayofweek WHILE( @@FETCH_STATUS = 0 ) BEGIN SET @ssum = @ssum + ',' + @dayofweek FETCH NEXT FROM crs INTO @dayofweek END CLOSE crs DEALLOCATE crs IF( LEN(@ssum) != 0 ) SET @ssum = SUBSTRING(@ssum,2,255) PRINT @ssum

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I need to do exactly the same thing, but my result needs to print in a textbox in a SQL Server 2000 Reporting Services Report. I created a function in the Custom Code area for the report, but I get the message: "Statement is not valid inside a method." Can you tell me how to make your code work in Reporting Services?:confused:

          A 1 Reply Last reply
          0
          • L Lost User

            I need to do exactly the same thing, but my result needs to print in a textbox in a SQL Server 2000 Reporting Services Report. I created a function in the Custom Code area for the report, but I get the message: "Statement is not valid inside a method." Can you tell me how to make your code work in Reporting Services?:confused:

            A Offline
            A Offline
            airbus380
            wrote on last edited by
            #5

            I want to say that you must create a "user defined function" (T SQL function) like this CREATE FUNCTION SSum (@id INT) //SSum = string sum RETURNS NVARCHAR(255) AS BEGIN DECLARE @ssum NVARCHAR(255) DECLARE @dayofweek NVARCHAR(25) SET @ssum = '' DECLARE crs CURSOR FOR SELECT DAYOFWEEK FROM schedule WHERE ID = @id OPEN crs FETCH NEXT FROM crs INTO @dayofweek WHILE( @@FETCH_STATUS = 0 ) BEGIN SET @ssum = @ssum + ',' + @dayofweek FETCH NEXT FROM crs INTO @dayofweek END CLOSE crs DEALLOCATE crs IF( LEN(@ssum) != 0 ) SET @ssum = SUBSTRING(@ssum,2,255) RETURN @ssum END After, built your report on a SELECT query using SSum function like this: SELECT DISTINCT ID , dbo.SSum(id) AS Days FROM schedule VB6,C# -- modified at 17:46 Wednesday 31st August, 2005

            L 1 Reply Last reply
            0
            • A airbus380

              I want to say that you must create a "user defined function" (T SQL function) like this CREATE FUNCTION SSum (@id INT) //SSum = string sum RETURNS NVARCHAR(255) AS BEGIN DECLARE @ssum NVARCHAR(255) DECLARE @dayofweek NVARCHAR(25) SET @ssum = '' DECLARE crs CURSOR FOR SELECT DAYOFWEEK FROM schedule WHERE ID = @id OPEN crs FETCH NEXT FROM crs INTO @dayofweek WHILE( @@FETCH_STATUS = 0 ) BEGIN SET @ssum = @ssum + ',' + @dayofweek FETCH NEXT FROM crs INTO @dayofweek END CLOSE crs DEALLOCATE crs IF( LEN(@ssum) != 0 ) SET @ssum = SUBSTRING(@ssum,2,255) RETURN @ssum END After, built your report on a SELECT query using SSum function like this: SELECT DISTINCT ID , dbo.SSum(id) AS Days FROM schedule VB6,C# -- modified at 17:46 Wednesday 31st August, 2005

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I was able to put your code into a stored procedure, then call the string from there. It worked!! The user defined function would probably work as well. Your code has saved me hours of work!:)

              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