Decrease Font Size
Increase Font Size
   BLOG

How to select multiple column values into a single row of string

" Parsing SQL column data into single string "

" SELECT column values into a concatenated string (SQL) "

" sql select column into single string"

" T-SQL - Select records into concatenated string" "how to combine string data from multiple columns into one column? "

Below is a simple example.

DECLARE @TempTable Table 
(
	col1 varchar(10),
	col2 char(1)
)

INSERT INTO @TempTable (col1, col2) values ('aa', 'Y')
INSERT INTO @TempTable (col1, col2) values ('bb', 'N')
INSERT INTO @TempTable (col1, col2) values ('cc', 'Y')
INSERT INTO @TempTable (col1, col2) values ('dd', 'N')
INSERT INTO @TempTable (col1, col2) values ('ee', 'N')
INSERT INTO @TempTable (col1, col2) values ('ff', 'Y')

--check table
SELECT * FROM @TempTable

DECLARE @my_long_string varchar(1000)
SET @my_long_string = ''

SELECT	@my_long_string = COALESCE(@my_long_string + ',', '') + col1
				FROM  @TempTable 
				WHERE col2 = 'Y'
				
SELECT substring(@my_long_string,CHARINDEX(',', @my_long_string)+1,len(@my_long_string)) 


Comments (6) -

  • health insurance forum

    8/18/2009 12:24:02 AM |

    There are certainly a lot of details like that to take into consideration. That’s a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be

  • Online dating

    11/4/2009 10:09:01 PM |

    Thank you for the work you have put into your nice blog. We will bookmark to your blog because it is very informational. We love the site and will come back to see your new posts.

  • bathrooms designs

    12/31/2009 3:04:41 PM |

    Hi

    Great choice for you to use BlogEngine.Net platform for your blog. I love blogengine too because its easy for me as newbie.
    Happy writing

    Regards,
    Joel

  • health cover

    1/31/2010 9:16:29 AM |

    Very useful info. Hope to see more posts soon!

  • vessel sinks

    3/6/2010 6:52:32 PM |

    select multiple column values into a single row of string are best part of it.Great point to share.

  • SEO

    3/11/2010 3:33:26 PM |

    Blogengine.net is really awesome thing to use in a website. It is easy to use and make changes in blog engengine.

Pingbacks and trackbacks (1)+

Add comment

Loading