xxxxxxxxxx
declare @text varchar(max) = 'BLACKHEATH 0AA BLACKHEATH COLCHESTER CO2 0AA'
declare @i int = 1;
declare @nextCharacter varchar(max)
declare @word varchar(max)=''
declare @lastChar varchar(1) = substring(@text,len(@text),1)
--select @lastChar
declare @lastIndex varchar(max) = charindex(@lastChar,@text,len(@text))
select @lastIndex
--select len(@text)
create table #tmp (id int,word varchar(max))
while (@i <= len(@text))
begin
select @nextCharacter= substring(@text, @i, 1)
--select @nextCharacter,@i, @lastChar, len(@text)
if (@nextCharacter !=' ')
begin
select @word = @word + @nextCharacter
end
else
begin
insert into #tmp
values(@i,@word)
set @word=''
end
if (@i = len(@text) and @nextCharacter= @lastChar)
begin
insert into #tmp
values(@i,@word)
end
set @i = @i +1
end;
select id,word from #tmp order by id;
WITH tblTemp as
(
SELECT ROW_NUMBER() Over(PARTITION BY word ORDER BY id)
As RowNumber,* FROM #tmp
) --select * from tblTemp
select * into #tmp2 FROM tblTemp where RowNumber =1
declare @newString varchar(max)=''
select @newString = @newString +word+' ' from #tmp2 order by id
select rtrim(@newString)
drop table #tmp2
drop table #tmp
xxxxxxxxxx
WITH cte AS (
SELECT
contact_id,
first_name,
last_name,
email,
ROW_NUMBER() OVER (
PARTITION BY
first_name,
last_name,
email
ORDER BY
first_name,
last_name,
email
) row_num
FROM
sales.contacts
)
DELETE FROM cte
WHERE row_num > 1;
/*
Code language: SQL (Structured Query Language) (sql)
In this statement:
First, the CTE uses the ROW_NUMBER() function to find the duplicate rows
specified by values in the first_name, last_name, and email columns.
Then, the DELETE statement deletes all the duplicate rows but keeps only one
occurrence of each duplicate group.*/
xxxxxxxxxx
DELETE FROM [SampleDB].[dbo].[Employee]
WHERE ID NOT IN
(
SELECT MAX(ID) AS MaxRecordID
FROM [SampleDB].[dbo].[Employee]
GROUP BY [FirstName],
[LastName],
[Country]
);