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 7827453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:52:55+00:00 2026-06-02T09:52:55+00:00

For a SQL Server 2005 Instance what is the best way to take find/replace

  • 0

For a SQL Server 2005 Instance what is the best way to take find/replace a column with multiple emails like

<JimmyTheBoot@yahoo.com>; JohnBlaze@TestMail.com; comfarmer@yahoo.com .....

and replace it with

<TestMail@yRandMail.com>; TestMail@RandMail.com; TestMail@RandMail.com .....

For testing purposes, I could think of some ways to do this in C# but I was wondering if there was a think way to do it in SQL Server, maybe with REGEX ? I want to keep the random weirdness as much as possible (some emails have brackets, some have semicolons at the end etc …)

Thanks

  • 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-02T09:52:59+00:00Added an answer on June 2, 2026 at 9:52 am

    Here you go, you can accomplish it with a cte in a function.

    create function dbo.FixupEmails(@s varchar(8000))
    returns table
    as
    return (
          WITH splitter_cte AS (
          SELECT CHARINDEX(';', @s) as pos, 0 as lastPos, 1 as cte_level
          UNION ALL
          SELECT CHARINDEX(';', @s, pos + 1), pos, cte_level + 1 as cte_level
          FROM splitter_cte
          WHERE pos > 0
          ), each_email_cte AS(
          select replace(replace(replace(OneEmail, '>', ''), '<', ''), ' ', '') as OneEmail, cte_level
            from (select SUBSTRING(@s, lastPos + 1,
                             case when pos = 0 then 80000 else pos - lastPos -1 end) as OneEmail,
                             cte_level
                    from splitter_cte) as t
          ), each_half_cte AS (
            select OneEmail, CHARINDEX('@', OneEmail) as atPos, cte_level
            from each_email_cte
            where len(OneEmail) > 6  -- 6 from x@x.co (I think that 6 would be the minimum valid email length)
          ), new_email_cte as
          (
            select cte1.OneEmail, Replace(@s, cte1.OneEmail, 'TestMail@RandMail.com') as New, cte1.cte_level --, 1 as level
            from each_half_cte cte1
            where cte1.cte_level = 1
    
            UNION ALL
    
            select cte2.OneEmail, Replace(necte.New, cte2.OneEmail, 'TestMail@RandMail.com') as New, cte2.cte_level--, 1 as level
            from new_email_cte as necte
            inner join each_half_cte as cte2 on cte2.cte_level = necte.cte_level + 1
    
    
          )
          select New
          from new_email_cte
          where cte_level = (select max(cte_level) from new_email_cte)
    )
    go
    
    set nocount on;
    
    declare @emailString varchar(2048)
    set @emailString = '<JimmyTheBoot@yahoo.com>; JohnBlaze@TestMail.com; comfarmer@yahoo.com ';
    select @emailString as Original;
    SELECT *
      FROM dbo.FixupEmails(@emailString);
    
    
    
    
    set @emailString = '<JimmyTheBoot@yahoo.com>; JohnBlaze@TestMail.com;';
    select @emailString as Original;
    SELECT *
      FROM dbo.FixupEmails(@emailString);
    
    
    set @emailString = '<JimmyTheBoot@yahoo.com>';
    select @emailString as Original;
    SELECT *
      FROM dbo.FixupEmails(@emailString)
    OPTION(MAXRECURSION 0);
    -- include MAXRECURSION as shown above if you have more than 100 email addresses in the field.
    
    
    
    set @emailString = '<bill@whatever.co.uk>; John@TestMail.tv;';
    select @emailString as Original;
    SELECT *
      FROM dbo.FixupEmails(@emailString)
    

    It is kind of long, but here is the output.

    Original
    ----------------------------------------------------------------
    <JimmyTheBoot@yahoo.com>; JohnBlaze@TestMail.com; comfarmer@yahoo.com 
    
    New
    -----------------------------------------------------------------
    <TestMail@RandMail.com>; TestMail@RandMail.com; TestMail@RandMail.com 
    
    
    
    
    Original
    ----------------------------------------------------------------
    <JimmyTheBoot@yahoo.com>; JohnBlaze@TestMail.com;
    
    New
    ----------------------------------------------------------------
    <TestMail@RandMail.com>; TestMail@RandMail.com;
    
    
    
    
    
    Original
    ----------------------------------------------------------------
    <JimmyTheBoot@yahoo.com>
    
    New
    ----------------------------------------------------------------
    <TestMail@RandMail.com>
    
    
    
    
    
    Original
    ----------------------------------------------------------------
    <bill@whatever.co.uk>; John@TestMail.tv;
    
    New
    ----------------------------------------------------------------
    <TestMail@RandMail.com>; TestMail@RandMail.com;
    

    This was lots of fun. I think that the function provided will do what you are looking for.

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

Sidebar

Related Questions

I have upgraded a SQL Server Express 2005 instance to Standard using the best
I'm enumerating all databases of an SQL Server 2005 instance using SMO like as
When using SQL Server Express 2005's User Instance feature with a connection string like
I have replication set up between a sql-server 2005 instance and multiple sql-server 2000
I have a script that connects to SQL Server 2005 default instance. But I'm
When I right-click the instance of a table on the SQL Server 2005, it
SQL Server 2005 x64 on Windows Server 2003 x64, with multiple instances (default +
On SQL Server 2005, I have a complex multi-level allocation process which looks like
I'm making a parameterized query using C# against a SQL server 2005 instance, and
I had to re-install my SQL Server 2005 Express instance due to a conflict

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.