Decrease Font Size
Increase Font Size
   BLOG

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

by Admin 8. July 2009 07:39

" 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)) 


Tags: , ,

SQL

Comments

8/20/2009 7:24:02 AM #

health insurance forum

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

health insurance forum United States

11/7/2009 5:09:01 AM #

Online dating

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.

Online dating United Kingdom

1/2/2010 10:04:41 PM #

bathrooms designs

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

bathrooms designs Chile

2/2/2010 4:16:29 PM #

health cover

Very useful info. Hope to see more posts soon!

health cover United States

3/9/2010 1:52:32 AM #

vessel sinks

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

vessel sinks United States

3/13/2010 10:33:26 PM #

SEO

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

SEO United States