2

I am migrating my database from MySQl to SQL Server. My app is built on top of drupal. I am not able to convert the following code to its SQL Server equivalent : addExpression("GROUP_CONCAT(qa.answer SEPARATOR ',') ", 'lookingfordetails').

What is the SQL Server equivalent of GROUP_CONCAT() and how do I implement it in addExpression()?

Hafsa Mushtaq
  • 2,591
  • 2
  • 8
  • 13
  • See https://stackoverflow.com/questions/17591490/how-to-make-a-query-with-group-concat-in-sql-server - note that it does depend on your version of SQL Server (it can be painful, but is always do-able). – MandyShaw Jul 13 '18 at 07:18
  • you can use Stuff function. [Check the below post](https://stackoverflow.com/questions/31211506/how-stuff-and-for-xml-path-work-in-sql-server) – Deepak Kumar Jul 13 '18 at 07:35
  • you can use Stuff function. [Check the below link for reference](https://stackoverflow.com/questions/44146920/sql-stuff-not-working-why) – Deepak Kumar Jul 13 '18 at 08:57

1 Answers1

1

I don't know if this works because I can't test it, but I suggest something like this:

$expression = 'STUFF((SELECT ',' + answer as lookingfordetails FROM table FOR XML PATH('')),1 ,1 ,'')';

$query->addExpression($expression);

I think you could see group_concat conversion to other databases: http://www.sqlines.com/mysql/functions/group_concat

Daniela
  • 454
  • 3
  • 9