Author |
Topic |
sigmas
Posting Yak Master
172 Posts |
Posted - 2013-01-11 : 09:03:56
|
Hi,A string sample value:declare @s varchar(50) = 'dups dups dups value1 value1 value3 value1 value dups'I want remove duplicate words in that string. so result must be:'dups value1 value3 value'Could you help me? |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2013-01-11 : 09:06:21
|
You can parse the string putting the words into a table then recreate the string using distinct values.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-11 : 09:08:36
|
similar tohttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=182027declare @s varchar(50) = 'dups dups dups value1 value1 value3 value1 value dups'declare @sunique varchar(50)SELECT DISTINCT @sunique= COALESCE(@sunique,'') + Val + ' 'FROM dbo.ParseValues(@s,' ') fSELECT @sunique ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
sigmas
Posting Yak Master
172 Posts |
Posted - 2013-01-11 : 09:16:29
|
Thanks,Splitting words is good. but is not another solution(optimized/intelligent) for this? I mean when even there are not duplicate words or just two duplicate word we need to split all of string. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-11 : 09:31:34
|
quote: Originally posted by sigmas Thanks,Splitting words is good. but is not another solution(optimized/intelligent) for this? I mean when even there are not duplicate words or just two duplicate word we need to split all of string.
Unless you parse string you cant determine whether it has duplicates. so anyways there should be parsing logic.Another way is thisdeclare @s varchar(50) = 'dups dups dups value1 value1 value3 value1 value dups'declare @sunique varchar(50)SELECT @sunique = COALESCE(@sunique,'') + vFROM(SELECT DISTINCT m.n.value('.','varchar(100)') + ' ' AS vFROM (SELECT CAST('<Values><Value>' + REPLACE(@s,' ','</Value><Value>') + '</Value></Values>' AS xml) AS u )tCROSS APPLY u.nodes('/Values/Value')m(n))rSELECT @sunique ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
sigmas
Posting Yak Master
172 Posts |
Posted - 2013-01-11 : 09:42:37
|
quote: Originally posted by visakh16
quote: Originally posted by sigmas Thanks,Splitting words is good. but is not another solution(optimized/intelligent) for this? I mean when even there are not duplicate words or just two duplicate word we need to split all of string.
Unless you parse string you cant determine whether it has duplicates. so anyways there should be parsing logic.Another way is thisdeclare @s varchar(50) = 'dups dups dups value1 value1 value3 value1 value dups'declare @sunique varchar(50)SELECT @sunique = COALESCE(@sunique,'') + vFROM(SELECT DISTINCT m.n.value('.','varchar(100)') + ' ' AS vFROM (SELECT CAST('<Values><Value>' + REPLACE(@s,' ','</Value><Value>') + '</Value></Values>' AS xml) AS u )tCROSS APPLY u.nodes('/Values/Value')m(n))rSELECT @sunique ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Could you explain this method? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-01-11 : 10:15:50
|
Its basically forming an xml document out of string of input data and then using T-sql XML functions to parse out data as table and applying distinct over it------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
sigmas
Posting Yak Master
172 Posts |
Posted - 2013-01-11 : 10:22:17
|
OK, if be some words same with '<values><value>' at the string then the query will return correct result or not? |
|
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2013-01-12 : 20:40:34
|
quote: Originally posted by sigmas OK, if be some words same with '<values><value>' at the string then the query will return correct result or not?
Not.Your turn. What would the source of such information with duplicated words be and why does it have the problem of producing duplicated words to begin with?--Jeff Moden RBAR is pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row".First step towards the paradigm shift of writing Set Based code:"Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." When writing schedules, keep the following in mind:"If you want it real bad, that's the way you'll likely get it." |
|
|
|