A while ago, I asked "How can I generate a breadcrumb of Categories in pure MySQL?", which a fellow stackoverflow member provided this neat code for my MySQL needs:
select group_concat(t2.name order by locate(concat('/', t2.id, '/'), concat(t1.path, '/')) separator ' - ') breadcrumb
from mdl_course_categories t1,
mdl_course_categories t2
where locate(concat('/', t2.id, '/'), concat(t1.path, '/'))
Today, I find I need an SQL Server 2016 solution to this combination of functions (group_concat()
and locate()
) to replicate this functionality. I tried running this code against this database, but I am hit with this error message instead:
SQLState: 42000
Error Code: 156
Message: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near the keyword 'order'.
I checked other questions, but was unable to transfer this knowledge to my needs. How can I replicate this functionality in SQL Server 2016?
Edit: I've read the links that @Bacon Bits provided (thank you!), but doing this in SQL Server 2016 seems as possible as programming a slice of cheese to fly to the moon. Unfortunately upgrading is not an option, so I'm stuck with this hot mess. All I need to do is replace the numbers in the path
column with the name
as per the id
. E.g.
| id | name | path | should display as |
|---------|---------------|-------------------|--------------------------------|
| 1 | Fruit and Veg | /1 | Fruit and Veg - Fruit |
| 436547 | Fruit | /1/436547 | Fruit and Veg - Fruit |
| 4657598 | Apples | /1/436547/4657598 | Fruit and Veg - Fruit - Apples |
SO FRUSTRATING! Here's my code so far:
select
stuff((',' + t2.name), 1, 1, charindex(concat('/', t2.id, '/'), concat(t1.path, '/')))
from prefix_course_categories t1,
prefix_course_categories t2
where charindex(concat('/', t2.id, '/'), concat(t1.path, '/'))
This produces the following error:
SQLState: 42000
Error Code: 4145
Message: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]An expression of non-boolean type specified in a context where a condition is expected, near 'ORDER'.
Help appreciated, thank you.