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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:22:02+00:00 2026-05-15T07:22:02+00:00

I have the below tables. tblInput Id WordPosition Words — ———– —– 1 1

  • 0

I have the below tables.

tblInput

Id  WordPosition    Words
--  -----------     -----
1   1               Hi
1   2               How
1   3               are
1   4               you
2   1               Ok
2   2               This
2   3               is
2   4               me

tblReplacement

Id  ReplacementWords
--- ----------------
1   Hi
2   are
3   Ok
4   This

The tblInput holds the list of words while the tblReplacement hold the words
that we need to search in the tblInput
and if a match is found then we need to replace
those.

But the problem is that, we need to replace those words if any match is found at the beginning.

i.e. in the tblInput,

in case of ID 1, the words that will be replaced is only 'Hi' and not 'are'

since before ‘are’, ‘How’ is there and it is not in the tblReplacement list.

in case of Id 2, the words that will be replaced are 'Ok' & 'This'. Since these both

words are present in the tblReplacement table and after the first word i.e. ‘Ok’ is

replaced, the second word which is ‘This’ here comes first in the list of

ID category 2

. Since it is available in the tblReplacement, and is the first word now, so this will

also be replaced.

So the desired output will be

Id  NewWordsAfterReplacement
--- ------------------------
1   How
1   are
1   you 
2   is
2   me

My approach so far:

;With Cte1 As(
Select 
    t1.Id
    ,t1.Words
    ,t2.ReplacementWords
From tblInput t1 
Cross Join tblReplacement t2)
,Cte2 As(
Select Id, NewWordsAfterReplacement = REPLACE(Words,ReplacementWords,'')
From Cte1)

Select * from Cte2 where NewWordsAfterReplacement <> ''

But I am not getting the desired output. It is replacing all the matching words.

Urgent help needed**.( SET BASED )**

I am using SQL Server 2005.

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-05-15T07:22:02+00:00Added an answer on May 15, 2026 at 7:22 am

    I couldnt exactly understand your requirement.But from what I understand this works for the given data

    with cte1 as(
    select 1 as id,   1 as wp, 'Hi' as words union all
    select 1,  2       ,        'How' union all
    select 1 ,  3   ,            'are' union all
    select 1 ,  4   ,            'you' union  all
    select 2 ,  1       ,        'Ok' union all
    select 2 ,  2 ,              'This' union all
    select  2 ,  3   ,            'is' union all
    select 2 ,  4       ,        'me'
    ),
     cte2 as(
    select 1 as id,  'Hi' as rep union all
    select 2 ,  'are' union all
    select 3 ,  'Ok' union all
    select 4  , 'This')
    
    select ID,words from cte1
    except
    select ID,words from(
    select a.id,a.words,a.wp,RANK() over(partition by a.id order by wp) as rnk
    from cte1 as a inner join
    cte2 as b on a.words=b.rep
    ) as x
    where x.wp=x.rnk
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have four tables like below in sql server 2008 : TABLE 1 ->
I have a problem. I have 3 tables like below: 1) met_ID (primair) etc.
I have two tables as shown below EmpMaster empid firstName lastName lstatus 1 Mark
I have three tables specifying important columns below Users(Id, username) Groups(Id, groupname, creator) (creator
I have 3 tables with values like below **tbl_product** recID pID price colour 1
I have two tables like the ones below. I need to find what exchangeRate
I have 10 tables in my database(MySQL). two of them is given below tbl_state
I have two tables names book_list and book_category . Schema is like below: book_list
I have three tables tag , page , pagetag With the data below page
I have below tables 1. Address -> AddressID 2. Designation -> DesignationID 3. Stores

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.