Blah Blah Blah : Paid by the line
Blah Blah Blah : The real WTF is <insert ignorant="" statement="" here=""><p>
<p>Yes... this is someone you should really learn from. If you can master this talented individuals' skills, you may even be able to work for Able's Car Wash writing a foxpro database to total daily inclome!</p>
Some of the lesser sins that would merely have his code bounced in code review:
Ordering on insert into a temp table (WHY??)
Casting join columns (Way to use indexes, dumbass)
In this case, however, I think the correct course of action is job retraining. Preferably something involving paper hats, a fryolator, and a requirement that he wear a badge that reads "TRAINEE" for the first year.
String Parsing is really not a good idea in SQL. Its a performance killer. This looks like something that would be better suited in the application code. If a lot of string parsing has to be done in sql, its a candidate for SQLCLR.
That being said. There is a much better way to do this...
That could actually make it worse. If this is SQL Server (which it looks like) then temporary tables are created in tempdb, which means that the script will fail if the account running it doesn't have Create Table permissions in tempdb.
Depends on the circumstances. There are better ways to do it on the SQL end as well. Under the circumstances however, given that it only transforms the output, it would definately be better to do this on the app end.