Separate words by group wise for each row in SQL

I have a string something like

No People,Day,side view,looking at camera,snow,mountain,tranquil scene,tranquility,Night,walking,water,Two People,looking Down

And I have a table Group_words

Group                                                                                                                                                                   
No People,One Person,Two Person,Three Person,Four Person,five person,medium group of people,large group of people,unrecognizable person,real people       
Day,dusk,night,dawn,sunset,sunrise                                                                                                                        
looking at camera,looking way,looking sideways,looking down,looking up                                                                                                          

I want to check every comma separated word with table Group_words and find the wrong combination.

For the above string result should be : "No People,Day,side view,looking at camera,snow,mountain,tranquil scene,tranquility,walking,water"

  • Night is removed because Day is available in the string.
  • Two People is removed because No People is available in the string.
  • looking Down is removed because looking at camera is available in the string.

I know its to complicated but simply I want to remove the not matching words from sting which is available into table Group_words.

Help is much appreciated. Thanks in advance . 

June 25th, 2015 2:32am

It is unclear, for example why you remove  Two People word and not No People, is it just because it occurred first?

I am not sure about a WHERE clause i put the below, but perhaps it gives you some idea to move on..

CREATE TABLE #group (id INT ,st VARCHAR(500))

                                                                                                                                                                
INSERT INTO #group VALUES (1,'No People,One Person,Two Person,Three Person,Four Person,five person,medium group of people,large group of people,unrecognizable person,real people')
INSERT INTO #group VALUES (2,'Day,dusk,night,dawn,sunset,sunrise')                                                                                                                        
INSERT INTO #group VALUES (3,'looking at camera,looking way,looking sideways,looking down,looking up')    


SELECT * FROM  #group    

DECLARE @st VARCHAR(500)='No People,Day,side view,looking at camera,snow,mountain,tranquil scene,tranquility,Night,walking,water,Two People,looking Down'

----No People,Day,side view,looking at camera,snow,mountain,tranquil scene,tranquility,walking,water


SELECT id,st,der.token,der1.token as token1  FROM #group
CROSS APPLY (SELECT * FROM [dbo].[SplitString] (st)) AS Der
CROSS APPLY (SELECT * FROM [dbo].[SplitString] (@st)) AS Der1
WHERE der.token<> der1.token





 CREATE FUNCTION [dbo].[SplitString]
     (
         @str VARCHAR(MAX)
     )
    RETURNS @ret TABLE (token VARCHAR(MAX))
     AS
     BEGIN
     
    DECLARE @x XML 
    SET @x = '<t>' + REPLACE(@str, ',', '</t><t>') + '</t>'
    
    INSERT INTO @ret
        SELECT x.i.value('.', 'VARCHAR(MAX)') AS token
        FROM @x.nodes('//t') x(i)
    RETURN
   END

Free Windows Admin Tool Kit Click here and download it now
June 25th, 2015 3:01am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics