Some general "Best Practice" advice:
1/. Never use a sql function call in a mapping. This will generate a round-trip to the database for every record that passes thru the data flow.
2/ Use lookup instead of lookup_ext whenever possible. It's slightly more efficient. And, most importantly, use the correct cache option (PRE_LOAD_CACHE or DEMAND_LOAD_CACHE, to avoid database round-trips, too)!
3/. Test your logic on a limited set of records, not on the full input data set. Until you've got it right. If it works for 10 records, it will probably work for all.
4/. Try to read and understand my answers to a post, first. Chances are high I am giving the right solution
.