Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Need unique record

e.g in the Name there is data as Nikhil Nikhil. I want output as Nikhil

Can you help me out please.

What I have tried:

-- Don't know how to get this one
Posted
Updated 14-May-18 18:54pm
Comments
RedDk 14-May-18 18:48pm    
Find the blank space. Then find either everything which leads up to it or everything which follows it. Or ... SELECT * FROM table WHERE [Name] LIKE '%Nikhil%'

Since there's only one row of data, there is no duplicate record.

Try this one
DECLARE @Table TABLE(Name varchar(100))
insert into @Table values('Nikhil Nikhil')
insert into @Table values('Nikhil Ram')
;with cte as (
select *,SUBSTRING(Name,0,CHARINDEX(' ',Name)) FirstName,SUBSTRING(Name,CHARINDEX(' ',Name)+1,LEN(Name)-CHARINDEX(' ',Name))LastName from @Table
)select Name Original_Name,case when FirstName=LastName then FirstName Else Name End New_Name from cte
 
Share this answer
 
Here can be possible solution,

-> Split your name by space and get array
-> using id create tabular form link[^]

example:
From->
||id||name|||
|1|nikhil nikhil|
|2|mohibur rashid|

To->
||id||name||
|1|nikhil|
|1|nikhil|
|2|mohibur|
|2|rashid|

-> then run select query to group all the name and rejoin using group by id.

I do not know whether it will keep your mohibur rashid as it is, or change to
rashid mohibur
 
Share this answer
 
It seems to me that you want to rework your DB to store the first and last names in two fields. String mashing in SQL is always a last resort. DISTINCT will give you unique records, but that's not really what you're asking
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900