@dmarkez asked a question yesterday, and just before I clicked the Post Your Answer button, he deleted the question. I think the answer is worth sharing… He didn’t re-post the question, so I hope he doesn’t mind if I re-post it so that I can share the answer with anyone else who is trying something similar:
Original Title
MS SQL function to manipulate string
Original Question
I need a MS SQL function that needs to determine names with prefix then combine this with actual names.
What is the best method or function to achieve this?
Prefix names: Te, De, Van, Dela, O, Mc, San, Los, etc…
Sample Input/Output Names:
van dam te mora te-> vandam temora te
o mara dela cruz -> omara delacruz
mc arthur white o san miguel -> mcarthur white osanmiguel
moana te aro van dolf-> moana tearo vandolf
If you put all the search & replace values in one table, then you can write a few lines of SQL to cycle through the S&R values to fix the names. It’s easy to add more S&R pairs to the
prefixtable, so your S&R routine is dynamic:Notice the
('san ', 'san')comes before('o ', 'o')in the prefix table. This is becausesanmust be replaced beforeo, orosanmiguelwill remainosan miguel. The order of the S&R is therefore important. You will need to add a clustered index to the prefix table that orders the S&R records correctly so that the S&R loop handles sub-prefixes first.