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

  • Home
  • SEARCH
  • 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 6881027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:03:19+00:00 2026-05-27T05:03:19+00:00

Since it’s not that easy to perform regex in SQL I’ll need some advice

  • 0

Since it’s not that easy to perform regex in SQL I’ll need some advice in how to solve this problem.

I have a column with the following type of data:

Lorem ipsum dolor sit $%foo##amet%$, consectetur adipiscing elit.
Nullam odio risus, mollis a interdum vitae, rutrum id leo. Pellentesque
dapibus lobortis mattis. Praesent at nisi a orci commodo scelerisque
$%bar##%$ eget id dui. Morbi est arcu, ultricies et consequat ac,
pretium sed mi. Quisque iaculis pretium congue. Etiam ullamcorper sapien
eu mauris tristique at venenatis mauris ultricies. Proin eu vehicula enim.
Vestibulum aliquam, mauris ac tempus vulputate, odio mauris rhoncus purus,
id suscipit velit erat quis magna.

The bold text I need to match, and it needs to be replaced with the text found in the second part.

Meaning:

  • $%foo##amet%$ becomes amet
  • $%bar##%$ becomes an empty string.

The pattern as a regex would be something like \$%[^#]+?##([^%]*?)%\$

I can’t really use that though since regex is not really supported in tsql…

Any advice?

  • 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-05-27T05:03:19+00:00Added an answer on May 27, 2026 at 5:03 am

    fixed it with this function:

    CREATE FUNCTION [dbo].[ReplaceWithDefault]
    (
       @InputString VARCHAR(4000)
    )
    RETURNS VARCHAR(4000)
    AS
    BEGIN
        DECLARE @Pattern VARCHAR(100) SET @Pattern = '$[%]_%##%[%]$'
        -- working copy of the string
        DECLARE @Result VARCHAR(4000) SET @Result = @InputString
        -- current match of the pattern
        DECLARE @CurMatch VARCHAR(500) SET @curMatch = ''
        -- string to replace the current match
        DECLARE @Replace VARCHAR(500) SET @Replace = ''
        -- start + end of the current match
        DECLARE @Start INT
        DECLARE @End INT
        -- length of current match
        DECLARE @CurLen INT
        -- Length of the total string -- 8001 if @InputString is NULL
        DECLARE @Len INT SET @Len = COALESCE(LEN(@InputString), 8001)
    
        WHILE (PATINDEX('%' + @Pattern + '%', @Result) != 0) 
        BEGIN
            SET @Replace = ''
    
            SET @Start = PATINDEX('%' + @Pattern + '%', @Result)
            SET @CurMatch = SUBSTRING(@Result, @Start, @Len)
    
            SET @End = PATINDEX('%[%]$%', @CurMatch) + 2
            SET @CurMatch = SUBSTRING(@CurMatch, 0, @End)
    
            SET @CurLen = LEN(@CurMatch)
    
            SET @Replace = REPLACE(RIGHT(@CurMatch, @CurLen - (PATINDEX('%##%', @CurMatch)+1)), '%$', '')
    
            SET @Result = REPLACE(@Result, @CurMatch, @Replace)
        END
        RETURN(@Result)
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

since they have same rendering engine, this problem shows in both. it works great
Since I'm pretty new this question'll certainly sound stupid but I have no idea
Since in the control file I have to provide a name (hard-coded) I need
Since I'm using Java 1.4.2, this means that I cannot use Java's implementation of
Since i'm writing a game in RoR, i need to have a game loop
Since I am new to using Unity 2.0, I need some pointer or suggestion
Since CS3 doesn't have a web service component, as previous versions had, is there
Since Rails is not multithreaded (yet), it seems like a threaded web framework would
Since MS will stop supporting anything before IE 8 soon, I am not sure
Since Windows doesnt have a C++ locale with UTF8 support by default, i would

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.