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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:01:19+00:00 2026-05-31T14:01:19+00:00

I need to find and replace an expression within a dynamic query. I have

  • 0

I need to find and replace an expression within a dynamic query. I have a subset of a where condition in string type like

'fieldA=23 OR field_1=300 OR fieldB=4'

What I need is to find a way to detect expression field_1=300 within the string and replace it while retaining the expression field_1=300.

I can do the detection part using CHARINDEX or PATINDEX but I’m not able to figure out how to use the patterns in the REPLACE function and how to get the value of the field_1 parameter.

Thanks in advance.

  • 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-31T14:01:21+00:00Added an answer on May 31, 2026 at 2:01 pm

    I’m not entirely clear on what you’re trying to acheieve (e.g. what are you wanting to replace “field_1=300” with, and is it the exact string “field_1=300” that you’re looking for, or just the field name, i.e. “field_1”?).

    Also, could you paste in the code you’ve written so far?

    Here’s a simple script which will extract the current value of a given field name:

    DECLARE @str VARCHAR(100),
            @str_tmp VARCHAR(100),
            @field_pattern VARCHAR(10),
            @field_val INT;
    
    SET @str = 'fieldA=23 OR field_1=300 OR fieldB=4';
    SET @field_pattern = 'field_1='
    
    -- This part will extract the current value assigned to the "@field_pattern" field
    IF CHARINDEX(@field_pattern, @str) > 0
    BEGIN
        SELECT @str_tmp = SUBSTRING(@str, 
                            CHARINDEX(@field_pattern, @str) + LEN(@field_pattern),
                            LEN(@str)
                            );
    
        SELECT @field_val = CAST(SUBSTRING(@str_tmp, 1, CHARINDEX(' ', @str_tmp)-1) AS INT);
    END
    
    PRINT @field_val
    

    If you want to replace the value itself (e.g. replacing “300” in this case with “600”), then you could add something like this:

    DECLARE @new_val INT;
    SET @new_val = 600;
    SET @str = REPLACE(@str, (@field_pattern+CAST(@field_val AS VARCHAR)), (@field_pattern+CAST(@new_val AS VARCHAR)));
    PRINT @str;
    

    Which would give you “fieldA=23 OR field_1=600 OR fieldB=4“.

    Cheers,
    Dave

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

Sidebar

Related Questions

I need to find two adjacent repeating digits in a string and replace with
I need a regular expression find and replace that I'm a little stuck on.
I have a regular expression for the Find and Replace window in Visual Studio
I am in need of a regular expression that will find, and replace, a
I need to perform a find and replace using XSLT 1.0 which is really
I need to do a find and replace in Notepad++ of Err.Number, canBeAnything, canBeAnything,
I need to do a regex find and replace on all the files in
I need to do a find and replace on about 45k lines of a
I need to combine Expression Engine with http://www.infinite-scroll.com . I have tried numerous ways
I have a user supplied string, that I then need to turn into a

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.