It's pretty nice to be able to do this from stored procedures, but from a performance point of view, wouldn't it make more sense to write it in a couple of lines of C? MySQL does support pluggable functions right?
If I remember correctly (and I probably don't), the C function support is a bit iffy.