I was struggling with how to tackle this type of problem in T-SQL until I came across your sample code Jeff.
What can I say?
A bloody marvelous and timeless piece of T-SQL. It does exactly what I want it to do and I've learned something new in the process.
Thank you.