[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL How do I concatenate strings from a column into a single row

Posted on 2009-04-10
4
Medium Priority
?
980 Views
Last Modified: 2012-05-06
The  column ProductType.searchtext in this query can have multiple values. I need to join the values together separated by a comma to insure there is always one row of data. I have done this in separate queries but cannot put it all together.
------------ Query One --------------
 
SELECT endshare.orderno, endshare.ship_via, endshare.nvalue, endshare.bill_weigh, endshare.sname, endshare.scompany, endshare.saddr1, endshare.saddr2, endshare.scity, endshare.sstate, endshare.szipcode, endshare.scountry, endshare.sphone1, endshare.semail, endshare.usi_status, endshare.ship_date, endshare.BOX_ID, endshare.ORDERNO, ProductType.searchtext
 
FROM endshare 
 
join items on items.orderno = endshare.orderno
join stock on items.item = stock.number
join ProductType on stock.assoc = ProductType.prodtype
 
WHERE endshare.orderno=318850
 
 
 
------------ Query Two --------------
DECLARE @searchtext VARCHAR(1024) 
 
SELECT 
    @searchtext = COALESCE(@searchtext + ',', '') + searchtext 
FROM 
    ProductType
 
SELECT ProductType = @searchtext

Open in new window

0
Comment
Question by:ubsmail
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Expert Comment

by:GarthSnyder
ID: 24120663
You will need to use a user-defined function, as you have done. Here is an example of combining the concatenation with other query results. See the second post, by TG.

This feature is built into MySQL in the form of the GROUP_CONCAT() aggregate function, so Googling for GROUP_CONCAT and "MS Sql" brings up a lot of good hits.

I would include exact code for you but I am not actually an MS Sql user and don't have a test database available.
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 1000 total points
ID: 24121046
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_24157447.html

I had a similar question.

Essentially I ended up with code like this ...
CREATE FUNCTION [dbo].[ListContractsForSingleReport](@i_ReportID INT, @s_Separator VARCHAR(MAX) = ',')
RETURNS VARCHAR(MAX)
AS
BEGIN
        DECLARE @s_List VARCHAR(MAX)
        SELECT @s_List = ''
        SELECT @s_List = @s_List + @s_Separator +  CAST(ContractID AS VARCHAR)
        FROM BANDIT.dbo.ContractReports
        WHERE @ i_ReportID  = ReportID 
        RETURN SubString(@s_List, 2, Len(@s_List))
END

Open in new window

0
 
LVL 27

Assisted Solution

by:Chris Luttrell
Chris Luttrell earned 1000 total points
ID: 24121168
Use an XML trick like this one from before http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_24310874.html
I believe the code below is correctly applied to your query.
SELECT endshare.orderno, endshare.ship_via, endshare.nvalue, endshare.bill_weigh, endshare.sname, endshare.scompany, endshare.saddr1, endshare.saddr2, endshare.scity, endshare.sstate, endshare.szipcode, endshare.scountry, endshare.sphone1, endshare.semail, endshare.usi_status, endshare.ship_date, endshare.BOX_ID, endshare.ORDERNO, ProductType = REPLACE((SELECT Names AS [data()]
                   FROM ProductType
                   WHERE stock.assoc = ProductType.prodtype
                   FOR XML PATH ('')), ' ', ',')
 FROM endshare 
join items on items.orderno = endshare.orderno
join stock on items.item = stock.number
join ProductType on stock.assoc = ProductType.prodtype
WHERE endshare.orderno=318850

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question