Concatenate grouped rows

Use: select t.id, sum(t.price) , stuff(( select distinct ‘,’ + cast(t2.ServiceID as varchar(max)) from @t t2 where t2.id = t.id for xml path(”) ), 1, 1, ”) from @t t group by t.id Output: ———– ——————— ——————— 1 40,00 11,12 2 120,00 11

Execute a XQuery with PHP

PHP does not have any native or common XML parsers that support XQuery (If I’m wrong, someone let me know). It does however have two pretty standard extensions that handle XPath queries. I personally think simplexml is the better of the two. You would simply use: $xml = new simplexml($some_xml_string); $xpath_results = $xml -> Xpath(“//a/b”); … Read more

SQL Server SELECT to JSON function

Starting from SQL Server 2016 you can use for json: declare @t table(id int, name nvarchar(max), active bit) insert @t values (1, ‘Bob Jones’, 1), (2, ‘John Smith’, 0) select id, name, active from @t for json auto With older versions of SQL Server you can use for xml path, e.g.: select ‘[‘ + STUFF(( … Read more

How can I query a value in SQL Server XML column

select Roles from MyTable where Roles.value(‘(/root/role)[1]’, ‘varchar(max)’) like ‘StringToSearchFor’ In case your column is not XML, you need to convert it. You can also use other syntax to query certain attributes of your XML data. Here is an example… Let’s suppose that data column has this: <Utilities.CodeSystems.CodeSystemCodes iid=”107″ CodeSystem=”2″ Code=”0001F” CodeTags=”-19-“…./> … and you only … Read more