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. Get Comma Separated List from Query

Get Comma Separated List from Query

Scheduled Pinned Locked Moved Database
databasehelptutorial
8 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.
  • S Offline
    S Offline
    sarah_chandran
    wrote on last edited by
    #1

    Can anyone tell me how to write a sql query to get a comma separated list of values. Eg. IF the table ColorTable contains in it's colors field red,blue and green. The query "Select colors from ColorTable" would return 3 rows with each row containing each color. I would like to have a query that would return the colors in one row as red,blue,green instead of returning 3 rows I tried using list(colors) but this function is not recognised. Kindly help me, Thanks in advance

    A S 2 Replies Last reply
    0
    • S sarah_chandran

      Can anyone tell me how to write a sql query to get a comma separated list of values. Eg. IF the table ColorTable contains in it's colors field red,blue and green. The query "Select colors from ColorTable" would return 3 rows with each row containing each color. I would like to have a query that would return the colors in one row as red,blue,green instead of returning 3 rows I tried using list(colors) but this function is not recognised. Kindly help me, Thanks in advance

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

      select ( (select description from colortable where description='green') + ',' + (select description from colortable where description='Blue') + ','+ (select description from colortable where description='Red') ) as colors from colortable

      S 1 Reply Last reply
      0
      • A albCode

        select ( (select description from colortable where description='green') + ',' + (select description from colortable where description='Blue') + ','+ (select description from colortable where description='Red') ) as colors from colortable

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

        Is there any other way without hard coding the values and individually concatenating them as I have to retrieve data from a table with numerous records based on some conditions.

        1 Reply Last reply
        0
        • S sarah_chandran

          Can anyone tell me how to write a sql query to get a comma separated list of values. Eg. IF the table ColorTable contains in it's colors field red,blue and green. The query "Select colors from ColorTable" would return 3 rows with each row containing each color. I would like to have a query that would return the colors in one row as red,blue,green instead of returning 3 rows I tried using list(colors) but this function is not recognised. Kindly help me, Thanks in advance

          S Offline
          S Offline
          Stephan Samuel
          wrote on last edited by
          #4

          You can do something like this: declare @list varchar(1000) select @list = isnull(@list, '') + colors + ',' from ColorTable select @list It's got some extra "features" like that it always ends in a comma, but for every issue you may have with it there's a reasonable solution. Ask if you need more help with that.

          S 1 Reply Last reply
          0
          • S Stephan Samuel

            You can do something like this: declare @list varchar(1000) select @list = isnull(@list, '') + colors + ',' from ColorTable select @list It's got some extra "features" like that it always ends in a comma, but for every issue you may have with it there's a reasonable solution. Ask if you need more help with that.

            S Offline
            S Offline
            sarah_chandran
            wrote on last edited by
            #5

            That was just what i needed. Thanks a lot. By using the following i have removed the comma that appears at the end of the line select substring(@list,1,len(@list)-1) Now can you please tell me if there is a way in which the comma before the last item can be replaced with 'or' i.e. I would like the output to be like red,blue,green,..........pink or yellow -- modified at 0:55 Thursday 8th June, 2006

            S 1 Reply Last reply
            0
            • S sarah_chandran

              That was just what i needed. Thanks a lot. By using the following i have removed the comma that appears at the end of the line select substring(@list,1,len(@list)-1) Now can you please tell me if there is a way in which the comma before the last item can be replaced with 'or' i.e. I would like the output to be like red,blue,green,..........pink or yellow -- modified at 0:55 Thursday 8th June, 2006

              S Offline
              S Offline
              Stephan Samuel
              wrote on last edited by
              #6

              Sure, try this: declare @list varchar(1000) declare @len int select @list = isnull(@list, '') + colors + ',', @len = len(colors) from ColorTable select @list = substring(@list, 1, len(@list)-1) select substring(@list, 1, len(@list) - @len - 1) + " or " + substring(@list, len(@list) - @len, @len) I'm not in front of a SQL Server right now so I can't try it easily, and I may be off by a character (and thus a -1 or +1 in the substring lengths) here and there. I'm sure you can figure out the tweaks, though. The concept is to have a variable hold the length of the last element. It does this by constantly replacing its value with the length of the current element; at the end of the query, it will contain the length of the last element. Once you know that, it's a matter of string handling to cut your list up and replace text as necessary. Two more things: 1. If there's a possibility that ColorTable may contain zero, one or two rows, or if you're using a where clause that may return only zero, one or two rows, you may want either some if/then or case when clauses with select count statements to ensure you don't return errors. 2. While it's possible to do this in SQL, as demonstrated, there are some things that are better left to code. I assume you have a really good reason for doing it on the SQL Server instead of in whatever language you're using for your queries, but in general I'd recommend this be done in a code library instead of on the DB server.

              S 2 Replies Last reply
              0
              • S Stephan Samuel

                Sure, try this: declare @list varchar(1000) declare @len int select @list = isnull(@list, '') + colors + ',', @len = len(colors) from ColorTable select @list = substring(@list, 1, len(@list)-1) select substring(@list, 1, len(@list) - @len - 1) + " or " + substring(@list, len(@list) - @len, @len) I'm not in front of a SQL Server right now so I can't try it easily, and I may be off by a character (and thus a -1 or +1 in the substring lengths) here and there. I'm sure you can figure out the tweaks, though. The concept is to have a variable hold the length of the last element. It does this by constantly replacing its value with the length of the current element; at the end of the query, it will contain the length of the last element. Once you know that, it's a matter of string handling to cut your list up and replace text as necessary. Two more things: 1. If there's a possibility that ColorTable may contain zero, one or two rows, or if you're using a where clause that may return only zero, one or two rows, you may want either some if/then or case when clauses with select count statements to ensure you don't return errors. 2. While it's possible to do this in SQL, as demonstrated, there are some things that are better left to code. I assume you have a really good reason for doing it on the SQL Server instead of in whatever language you're using for your queries, but in general I'd recommend this be done in a code library instead of on the DB server.

                S Offline
                S Offline
                sarah_chandran
                wrote on last edited by
                #7

                Thanks a lot for your help. Thanks a lot for taking the time to reply.

                1 Reply Last reply
                0
                • S Stephan Samuel

                  Sure, try this: declare @list varchar(1000) declare @len int select @list = isnull(@list, '') + colors + ',', @len = len(colors) from ColorTable select @list = substring(@list, 1, len(@list)-1) select substring(@list, 1, len(@list) - @len - 1) + " or " + substring(@list, len(@list) - @len, @len) I'm not in front of a SQL Server right now so I can't try it easily, and I may be off by a character (and thus a -1 or +1 in the substring lengths) here and there. I'm sure you can figure out the tweaks, though. The concept is to have a variable hold the length of the last element. It does this by constantly replacing its value with the length of the current element; at the end of the query, it will contain the length of the last element. Once you know that, it's a matter of string handling to cut your list up and replace text as necessary. Two more things: 1. If there's a possibility that ColorTable may contain zero, one or two rows, or if you're using a where clause that may return only zero, one or two rows, you may want either some if/then or case when clauses with select count statements to ensure you don't return errors. 2. While it's possible to do this in SQL, as demonstrated, there are some things that are better left to code. I assume you have a really good reason for doing it on the SQL Server instead of in whatever language you're using for your queries, but in general I'd recommend this be done in a code library instead of on the DB server.

                  S Offline
                  S Offline
                  sarah_chandran
                  wrote on last edited by
                  #8

                  Thanks.It's just what I needed.

                  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