Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

Creating a comma-separated list (SQL Spackle) Expand / Collapse
Author
Message
Posted Tuesday, January 11, 2011 8:11 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:55 PM
Points: 6,582, Visits: 8,860
Comments posted to this topic are about the item Creating a comma-separated list (SQL Spackle)

Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1046233
Posted Wednesday, January 12, 2011 12:18 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, July 17, 2014 11:18 PM
Points: 15, Visits: 116
Thank you for this tip with the STUFF function.

Is the order by in the subquery really needed? I think the for xml does it anyways. If you don't use the order by; you may use distinct to get every value only once.

WITH CTE AS
(
SELECT DISTINCT
AccountNumber
FROM #TestData
)
SELECT AccountNumber,
CommaList = STUFF((
SELECT distinct ',' + Value --<<-- to get every value only once
FROM #TestData
WHERE AccountNumber = CTE.AccountNumber
--ORDER BY Value --<<--
FOR XML PATH(''),
TYPE).value('.','varchar(max)'),1,1,'')
FROM CTE
ORDER BY AccountNumber;

greetings Reto E.
Post #1046291
Posted Wednesday, January 12, 2011 3:56 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:55 PM
Points: 6,582, Visits: 8,860
reto.eggenberger (1/12/2011)
Thank you for this tip with the STUFF function.

Is the order by in the subquery really needed? I think the for xml does it anyways. If you don't use the order by; you may use distinct to get every value only once.


The order by in the subquery is used to control the ordering of the elements in the XML. If you don't use it, then there is no guarantee as to the order. You can use the distinct (or group by) to get each value once. This is independent of the order by clause.


Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1046369
Posted Wednesday, January 12, 2011 5:09 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, November 23, 2012 6:26 AM
Points: 1, Visits: 21
This is exactly the situation where I use PowerShell frequently. I am able to export data from whatever complex query. Idea is something like this:

Invoke-Sqlcmd -ServerInstance myServer -Database MyDb -Query "SELECT * FROM SomeTable" | Export-Csv ./result.csv -NoTypeInformation

In -Query can be any T-SQL code and results are exported natively to CSV file. I work as ConfigMgr admin and this is the way how you can really easily receive your data.

David
Post #1046402
Posted Wednesday, January 12, 2011 6:03 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Saturday, May 31, 2014 10:37 PM
Points: 85, Visits: 625
This is useful. I only have to do this once in a while, and I've been using clumsier methods.
Post #1046431
Posted Wednesday, January 12, 2011 6:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:55 PM
Points: 6,582, Visits: 8,860
glock 71629 (1/12/2011)
This is exactly the situation where I use PowerShell frequently. I am able to export data from whatever complex query. Idea is something like this:

Invoke-Sqlcmd -ServerInstance myServer -Database MyDb -Query "SELECT * FROM SomeTable" | Export-Csv ./result.csv -NoTypeInformation

In -Query can be any T-SQL code and results are exported natively to CSV file. I work as ConfigMgr admin and this is the way how you can really easily receive your data.

David


David,
I agree that there are several ways (BCP, PowerShell, etc.) to export the results of a query to a csv file. However, this article is about building a column of comma-separated values as part of a result set... which is completely different.


Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1046450
Posted Wednesday, January 12, 2011 7:16 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 8, 2014 6:13 AM
Points: 1,694, Visits: 19,550
Changing

value('.','varchar(max)'),1,1,'')

to

value('(./text())[1]','varchar(max)'),1,1,'')

appears to give a better query plan

Great article BTW.


____________________________________________________

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1046467
Posted Wednesday, January 12, 2011 7:27 AM


Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, March 8, 2011 1:45 PM
Points: 5, Visits: 36
To all,

Here is an alternative to creating a Comma delimit list directly from a table.

-- Create a Comma Delimited List of Client Numbers (BclCode] in
-- @locBclCodeList from dbo.SomeTable

Declare @locBclCodeList Varchar(Max)
SELECT @locBclCodeList =
COALESCE(LTrim(RTrim(@locBclCodeList)) + ',' ,'') + [SomeColumn]
From dbo.SomeTable

-- Joins and Where clauses can go here if you need them
Print ' @locBclCodeList [' + @locBclCodeList + ']'

Rex M Haverty, MCP, DBA
Post #1046476
Posted Wednesday, January 12, 2011 8:14 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Monday, July 14, 2014 10:08 AM
Points: 754, Visits: 3,816
Wayne, yes! More like this. This is right on target.

______________________________________________________________________
The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge. - Stephen Hawking
Post #1046520
Posted Wednesday, January 12, 2011 8:14 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:55 PM
Points: 6,582, Visits: 8,860
Rex - yes, that way will work. However, please see this article for performance differences between the two methods. String manipulation has never been a strong area for MS, and your method would suffer if there is a lot of data being put into that string.

Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1046521
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse