none
SQL group_concat function in SQL Server RRS feed

  • Question

  • Hi,

    How to solve Mysql  ‘group_concat’ function  in sql server?

    Please help me by giving some example query.

     

    Thanks and Regards

    Sam

    Friday, September 6, 2013 7:07 AM

Answers

  • Is this equivalent to MySQL "group_concate" function ?

    DECLARE @TEMP TABLE([VALUE] NVARCHAR(30))
    INSERT INTO @TEMP VALUES('MAZ')
    INSERT INTO @TEMP VALUES('HON')
    INSERT INTO @TEMP VALUES('FOR')
    INSERT INTO @TEMP VALUES('JEEP')
    INSERT INTO @TEMP VALUES('CHE')
    INSERT INTO @TEMP VALUES('NIS')
    INSERT INTO @TEMP VALUES('GMC')
    INSERT INTO @TEMP VALUES('ACC')
    INSERT INTO @TEMP VALUES('LEX')
    ------------------
    SELECT top 1 Stuff((SELECT ',' + [VALUE] 
                  FROM   @TEMP 
                  FOR xml path('')), 1, 1, '') [VALUE]
    FROM   @TEMP a 


    Regards, RSingh

    Friday, September 6, 2013 10:43 AM

All replies

  • Hi Sam,

    I believe the only way to do this in SQL Server is to use a variable such as this:

    declare @table table
    (
    	value varchar(10)
    )
    insert into @table
    values('a'), ('b'), ('c')
    
    declare @string varchar(10)
    
    select @string = isnull(@string + ', ', '') + value
    from @table
    
    select @string

    Friday, September 6, 2013 7:36 AM
  • If you are using SQL Server 2012 there is a new function called CONCAT (No need to check for NULLs)

    Best Regards,Uri Dimant SQL Server MVP, http://sqlblog.com/blogs/uri_dimant/

    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Blog: Large scale of database and data cleansing
    Remote DBA Services: Improves MS SQL Database Performance

    Friday, September 6, 2013 8:05 AM
    Answerer
  • check this link for group concat in sql server

    http://groupconcat.codeplex.com/


    Ramesh Babu Vavilla MCTS,MSBI

    Friday, September 6, 2013 8:50 AM
  • Is this equivalent to MySQL "group_concate" function ?

    DECLARE @TEMP TABLE([VALUE] NVARCHAR(30))
    INSERT INTO @TEMP VALUES('MAZ')
    INSERT INTO @TEMP VALUES('HON')
    INSERT INTO @TEMP VALUES('FOR')
    INSERT INTO @TEMP VALUES('JEEP')
    INSERT INTO @TEMP VALUES('CHE')
    INSERT INTO @TEMP VALUES('NIS')
    INSERT INTO @TEMP VALUES('GMC')
    INSERT INTO @TEMP VALUES('ACC')
    INSERT INTO @TEMP VALUES('LEX')
    ------------------
    SELECT top 1 Stuff((SELECT ',' + [VALUE] 
                  FROM   @TEMP 
                  FOR xml path('')), 1, 1, '') [VALUE]
    FROM   @TEMP a 


    Regards, RSingh

    Friday, September 6, 2013 10:43 AM