Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Nakul Vachhrajani is a Technical Specialist & Systems development professional with iGATE. He holds a MCTS (SQL Server 2008: Implementation & Maintenance)
Browse by Tags · View All
#SQLServer 242
SQL Server 241
Administration 208
DBA 197
Tips 186
Development 186
T-SQL 181
#TSQL 179
Guidance 123
Tools and Utilities 112

Archive · View All
April 2011 14
March 2012 11
December 2011 11
March 2011 11
December 2012 10
October 2011 10
January 2011 10
January 2013 9
November 2012 9
October 2012 9

SQL Server – T-SQL – Different ways to generate a comma-separated string from a table

Dec 29 2011 12:05AM by Nakul Vachhrajani   

Recently, someone in the team faced a fairly common requirement - to generate a comma-separated string from values stored in a table. This being the last post of the year, I thought of sharing the 2 most-commonly used methods I know of implementing this requirement.

Do you know any other? If you share it on this post, I will publish it with due credit on my blog.

----------------------------------------------------
--WARNING: THIS SCRIPT IS PROVIDED AS-IS AND WITHOUT
--         WARRANTY.
--         FOR DEMONSTRATION PURPOSES ONLY       
----------------------------------------------------
--Step 01: Generate Temp table to store source data
DECLARE  @NamesTable TABLE (Id INT,
                            Name NVARCHAR(50))

--Step 02: Generate test data
INSERT INTO @NamesTable VALUES (1,'A'),
                               (2,'D'),
                               (2,'C'),
                               (3,'E'),
                               (3,'H'),
                               (3,'G')

--Option 01: My favourite
DECLARE @listStr VARCHAR(MAX) --DO NOT initialize this one!

SELECT @listStr = COALESCE(@listStr + ',' ,'') + nt.Name
FROM @NamesTable nt

SELECT @listStr

--Option 02: Using XML
; WITH CommaSeparatedXML (CommaSeparatedXML)
AS (SELECT CAST((SELECT (',' + nt.Name)
                 FROM @NamesTable nt
                 FOR XML PATH('')) AS NVARCHAR(MAX))
   )
SELECT SUBSTRING(CommaSeparatedXML, 2, LEN(CommaSeparatedXML))
FROM CommaSeparatedXML
GO

Until we meet next time,

Be courteous. Drive responsibly.

Subscribe to my posts via E-mail: Subscribe here | Read my posts via your favourite RSS reader: Click Here!

Tags: Development, T-SQL, Tips, SQL Server, #SQLServer, #TSQL, Best Practices, Guidance, Holidays,


Nakul Vachhrajani
4 · 33% · 10680
3
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

7  Comments  

  • Another option is to use PIVOT. Not nearly as flexible as the FOR XML PATH('') solution

    DECLARE @NamesTable TABLE (Id INT, Name NVARCHAR(50))
    
    INSERT INTO @NamesTable (Id, Name)
    VALUES 
        (1, 'A'), 
        (2, 'D'), 
        (2, 'C'), 
        (3, 'E'), 
        (3, 'H'), 
        (3, 'G')
    
    SELECT
        ISNULL(CAST([1] as varchar(max)) + ',', '') + 
        ISNULL(CAST([2] as varchar(max)) + ',', '') + 
        ISNULL(CAST([3] as varchar(max)) + ',', '') + 
        ISNULL(CAST([4] as varchar(max)) + ',', '') + 
        ISNULL(CAST([5] as varchar(max)) + ',', '') + 
        ISNULL(CAST([6] as varchar(max)), '') 
    FROM
        (SELECT ROW_NUMBER() OVER (ORDER BY Name) as RowNum, Name FROM @NamesTable) Base
        PIVOT
        	(
        		MIN(Name)
        		FOR RowNum IN ([1],[2],[3],[4],[5], [6])
        	) pvt
    
    commented on Jan 16 2012 9:34AM
    Marc Jellinek
    97 · 2% · 556
  • Using CTE you can avoid using variable declaration.

    ;WITH CTE(x, name )
    AS
    (SELECT ROW_NUMBER() OVER (ORDER BY Id ), NAME FROM @NamesTable )
    ,CSV(X,Result)
    AS
    (SELECT x, CONVERT(varchar(max),NAME) FROM CTE WHERE x = 1
    UNION ALL 
    SELECT CSV.x+1, CONVERT(varchar(max),Result +',' + NAME )
    FROM CSV 
    INNER JOIN CTE 
    ON CSV.X+1 = CTE.x )
    SELECT Result FROM CSV T1
    WHERE NOT exists (SELECT 1 FROM CSV T2 WHERE T1.X < T2.X )
    
    commented on Jan 16 2012 10:43AM
    Eric Bradford
    72 · 2% · 775
  • Another possibility is to avoid both the variable AND the CTE:

    SELECT STUFF((SELECT (',' + nt.Name)
              FROM @NamesTable nt
              FOR XML PATH('')), 1, 1, '');
    
    commented on Jan 17 2012 8:20AM
    Aaron N. Cutshall
    704 · 0% · 46
  • Great feedback, everyone! Thank-you very much!

    @marc_jellinek: PIVOT is possible, but here's the issue. Conventional PIVOT would work great for a fixed number of strings. The option is to use dynamic PIVOT, which would be perhaps be the costliest of all operations. @Eric Bradford: Great idea on using the CTE. I will try it out and compare it with the others from a performance perspective. @Aaron N. Cutshall: Using XML is a good option, but I find it to be costly for this task. It does make for a compact, clean code, though.

    Keep the feedback coming, and have a nice day!

    commented on Jan 17 2012 1:00PM
    Nakul Vachhrajani
    4 · 33% · 10680
  • Nakul: Your comment about XML is valid enough and the STUFF operation on top of that does take additional time. I agree that the COALESCE option is the most efficient but only when you have the luxury to use a variable. In most cases when I've had a similar need I needed the results to be from a query and the variable option was not possible.

    commented on Jan 17 2012 1:50PM
    Aaron N. Cutshall
    704 · 0% · 46
  • @Aaron N. Cutshall: Absolutely - if the luxury of using a variable is not available, then using XML operations is a good approach.

    commented on Jan 17 2012 11:57PM
    Nakul Vachhrajani
    4 · 33% · 10680

Your Comment


Sign Up or Login to post a comment.

"SQL Server – T-SQL – Different ways to generate a comma-separated string from a table" rated 5 out of 5 by 3 readers
SQL Server – T-SQL – Different ways to generate a comma-separated string from a table , 5.0 out of 5 based on 3 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]