Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8512407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:16:19+00:00 2026-06-11T04:16:19+00:00

@dmarkez asked a question yesterday, and just before I clicked the Post Your Answer

  • 0

@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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:16:21+00:00Added an answer on June 11, 2026 at 4:16 am

    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 prefix table, so your S&R routine is dynamic:

    declare @prefix table (srch varchar(255), rplc varchar(255))
    declare @names table (name varchar(255))
    
    insert into @prefix
    values ('te ', 'te'), ('de ', 'de'), ('van ', 'van'), ('dela ', 'dela'), ('san ', 'san'), ('o ', 'o'), ('mc ', 'mc'), ('los ', 'los')
    
    insert into @names
    values ('van dam te mora te'), ('o mara dela cruz'), ('mc arthur white o san miguel'), ('moana te aro van dolf')
    
    while (1=1)
    begin
      update n
      set    n.name = replace(n.name, p.srch, p.rplc)
      from   @names n,
             @prefix p
      where  (n.name like p.srch + '%') or (n.name like '% ' + p.srch + '%')
    
      if @@rowcount = 0
        break
    end
    
    select * from @names
    

    Notice the ('san ', 'san') comes before ('o ', 'o') in the prefix table. This is because san must be replaced before o, or osanmiguel will remain osan 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a follow-up question to my previous question: Cancel an already executing task

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.