I’ve been working on a stored procedure and hit a point where I have two really horrid lines. Is there a way to re-write this in a clearer way within the stored procedure? If not, how would I go about creating a function to do this?
, REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(l.tenant_trading_name,'~','-'), '"','-'), '#','-'), '%','-'), '*','-'), ':','-'), '<','-'), '>','-'), '?','-'), '/','-'), '\','-'), '{','-'), '|','-'), '}','-') as trading_name
, REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(l.suite_name,'&','-'), '~','-'), '"','-'), '#','-'), '%','-'), '*','-'), ':','-'), '<','-'), '>','-'), '?','-'), '/','-'), '\','-'), '{','-'), '|','-'), '}','-') as suite_name
Well you could simply have a function that does the dirty work for you:
Then you can say:
This at least abstracts the ugly
REPLACE()calls out of the procedure.(Note that I didn’t quite parse the entire line to see if there were other differences but it seemed to me that the only difference was the suite_name couldn’t have an & but the trading name could.)
Another way would be to store your “bad” characters in a table, making maintenance of those replacements a little easier (and once the table is populated, making the function much cleaner as well).
Now you can have your function simply say: