2

@strA and @strB are two variables

@strA = "055367911126753316"
@strB = "00055367"

how to find the common part "055367" and remove it from string A, using SQL server query without looping? the result should be "911126753316"

be noted that always string A begin by the end part of string B

5
  • They have other common parts also, for example "6", what to do with another common parts? Commented May 10, 2017 at 9:21
  • always string A begin by the end part of string B Commented May 10, 2017 at 9:29
  • But String b starts with '000' but in your common part it is 1 '0', Do we need to leave first two 0's of string b? Commented May 10, 2017 at 9:41
  • 1
    The question is not clear, there are so many ways of matching but you have to clear that in which way you want to match ? Commented May 10, 2017 at 9:44
  • the result in my example should be "911126753316" because the common part is "055367" Commented May 10, 2017 at 9:57

5 Answers 5

1

You can use replace in sql server as below:

declare @strA varchar(50) = '055367911126753316'
declare @strB varchar(50) = '00055367'

select replace(@strA,right(@strB,len(@strB)-2),'')

If it is in different columns in a table you can use as below:

create table #yourcolumns ( cola varchar(50), colb varchar(50))

insert into #yourcolumns (cola, colb) values
('055367911126753316', '00055367')

select replace(cola,right(colb,len(colb)-2),'') from #yourcolumns

I think we need to go for substring in your case as you are looking for startswith

select SUBSTRING(cola,CHARINDEX(LEFT(REVERSE(colb),1),cola)+1,len(cola)) from #yourcolumns
Sign up to request clarification or add additional context in comments.

3 Comments

where are you matching the string ?? what if @strB is 055367
actually, A and B represent two columns in a table and A could be '55367911126753316' in this case your function will not help
what if I have this: insert into #yourcolumns (cola, colb) values ('055367911126753316', '00055367') insert into #yourcolumns (cola, colb) values ('55367911126753316', '00055367')
0

Try This, works dynamically.

declare @StrA Nvarchar(250)='055367911126753316',@StrB Nvarchar(250)='0055367'

select right(@StrA,len(@strA)-PATINDEX('%[^'+@StrB+']%',@strA)+1)

Comments

0

It YES with looping, But it work....

create function MyFn (@A nvarchar(max), @B nvarchar(max))
returns  nvarchar(max)
as
begin

    declare  @I int, @L int, @SUB nvarchar(max)

    select @I = 1, @L =  len(@B)
    while @I<@L begin
        set @SUB = substring(@B,@I,@L-@I+1)
        if charindex(@SUB,@A,1) > 0 begin
            set @A = replace(@A, @SUB, '')
            break
        end
    set @I = @I + 1
    end

    return @A
end

Comments

0

If each string A starts with 000 and each string b just contains one 0 this should retrieve the correct result

WITH CTE AS (SELECT [StringA], [StringB],REPLACE([StringB], '000', '0') [tbd] 
FROM YourTable)

SELECT REPLACE(YourTable.[StringA], CTE.[tbd], ''), YourTable.[StringA]
FROM YourTable
JOIN CTE ON YourTable.[StringA]= CTE.[StringA]

Comments

0
DECLARE @strA varchar(50) = '055367911126753316'
DECLARE @strB varchar(50) = '00055367'

SET  @strA=(SELECT REVERSE(SUBSTRING(@strA, PATINDEX('%[^0 ]%', @strA + ' '), LEN(@strA))))
SET  @strB=(SELECT REVERSE(SUBSTRING(@strB, PATINDEX('%[^0 ]%', @strB + ' '), LEN(@strB))))

SELECT @strA String ,@strB StringtoSearch,REVERSE(SUBSTRING(@strA,0,CHARINDEX(RIGHT(@strA,LEN(@strB)),@strA)))ExpectedOutput

Output

ExpectedOutput
---------------
911126753316

1 Comment

you are searching for last character in strB in strA in our example is 7, but this number could be repeated. your example doesn't work on strA = '0575367911126753316' strB= '000575367'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.