Surely the real lesson from this should be that if something is this hard in SQL you should be doing it in a different language?
I rarely come across an application that is written purely in SQL, so there is usually a presentation layer that is more capable of handling what looks like a purely presentational issue.