Avatar of naisnet
naisnetFlag for United States of America

asked on 

T-SQL Compare two Strings and remove duplicates between them

I have a web search function that takes a first and last name and searches it against a full text catalog of names.  The issue I'm having is that I need to compare both strings and remove duplicate words between them without permanently concatenating them.  It doesn't matter which string is the reference and which is the target.

INPUT:

   FIRSTNAME: Jean ValJean
   LASTNAME: Smith Jean



DESIRED RESULT:

   FIRSTNAME: Jean ValJean
   LASTNAME: Smith

   -or-

   FIRSTNAME: ValJean
   LASTNAME: Smith Jean



Thanks!

Microsoft SQL ServerMicrosoft SQL Server 2008Microsoft SQL Server 2005

Avatar of undefined
Last Comment
naisnet
ASKER CERTIFIED SOLUTION
Avatar of Ebcidic
Ebcidic
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Some Typos in earlier post

declare @FIRSTNAME nvarchar(100), @LASTNAME nvarchar(100), @NEWFIRSTNAME nvarchar(100), @NEWLASTNAME nvarchar(100)
declare @F1 nvarchar(50),@F2 nvarchar(50),@L1 nvarchar(50),@L2 nvarchar(50)

set @FIRSTNAME= 'Jean ValJean'
SET @LASTNAME= 'Smith Jean'

SET @F1=SUBSTRING(@FIRSTNAME, 1, CHARINDEX(' ', @FIRSTNAME) - 1)
SET @F2=SUBSTRING(@FIRSTNAME, CHARINDEX(' ', @FIRSTNAME) + 1, LEN(@FIRSTNAME))

SET @L1=SUBSTRING(@LASTNAME, 1, CHARINDEX(' ', @LASTNAME) - 1)
SET @L2=SUBSTRING(@LASTNAME, CHARINDEX(' ', @LASTNAME) + 1, LEN(@LASTNAME))

if (@F1=@L2)
BEGIN
SET @NEWLASTNAME=@L1
--@NEWFIRSTNAME=@F2
END
PRINT @NEWLASTNAME
Avatar of naisnet
naisnet
Flag of United States of America image

ASKER

I actually ended up taking care of it by listing both strings into a table variable and then doing a select distinct.  Thank you both - I'm assigning the points half and half.

Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo