Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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 @tselect 'help', 'helpme' union allselect 'please', 'pleasethankyou' union allselect 'very', 'much' select col1, col2, replace(col2, col1, '') as col3from @t--------------------------------------------------S.Ahamed
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
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 !