Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Replacing partially duplicated string ?

Author  Topic 

Windza
Yak Posting Veteran

61 Posts

Posted - 2008-11-25 : 22:17:31
I have two columns (A & B) containing strings of which one (B) sometimes has the duplicate of A with an appended string...
e.g.
|-----A-----|-----B---------|
|help-------|helpme---------|
|please-----|pleasethankyou-|
|very-------|much-----------|

What I'm hoping for is a query to check for partial duplication and create another column (C) that displays B - A where a duplication is detected, or else simply duplicates B...

i.e.
|-----A-----|-----B---------|----C----|
|help-------|helpme---------|me-------|
|please-----|pleasethankyou-|thankyou-|
|very-------|much-----------|much-----|

Is this type of manipulation achievable with string fields ?
As always - any assistance is greatly appreciated..
Regards,
Windza

pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2008-11-25 : 22:41:20
it is easy...
declare @t table(Col1 varchar(50), col2 varchar(50))
insert @t
select 'help', 'helpme' union all
select 'please', 'pleasethankyou' union all
select 'very', 'much'


select col1, col2,
replace(col2, col1, '') as col3
from @t

--------------------------------------------------
S.Ahamed
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-25 : 23:29:26
Another way
SELECT A,B,COALESCE(STUFF(B,PATINDEX('%'+A+'%',B),LEN(A),''),B) AS C FROM @test
Go to Top of Page

Windza
Yak Posting Veteran

61 Posts

Posted - 2008-11-25 : 23:40:46
My word - that was easy... thank-you. I think I made the assumption it wasn't going to be easy and looked only for a difficult answer !
Go to Top of Page
   

- Advertisement -