Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I do not think you can do this with a SELECT. The best chance is to write a user defined function that returns a table with all the words and then do SELECT DISTINCT on it.</p> <hr> <p><strong>Disclaimer:</strong> Function <strong>dbo.Split</strong> is from <a href="http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648" rel="noreferrer">http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648</a></p> <pre><code>CREATE TABLE test ( id int identity(1, 1) not null, description varchar(50) not null ) INSERT INTO test VALUES('The dog jumped over the fence') INSERT INTO test VALUES('The giant tripped on the fence') CREATE FUNCTION dbo.Split ( @RowData nvarchar(2000), @SplitOn nvarchar(5) ) RETURNS @RtnValue table ( Id int identity(1,1), Data nvarchar(100) ) AS BEGIN Declare @Cnt int Set @Cnt = 1 While (Charindex(@SplitOn,@RowData)&gt;0) Begin Insert Into @RtnValue (data) Select Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1))) Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData)) Set @Cnt = @Cnt + 1 End Insert Into @RtnValue (data) Select Data = ltrim(rtrim(@RowData)) Return END CREATE FUNCTION dbo.SplitAll(@SplitOn nvarchar(5)) RETURNS @RtnValue table ( Id int identity(1,1), Data nvarchar(100) ) AS BEGIN DECLARE My_Cursor CURSOR FOR SELECT Description FROM dbo.test DECLARE @description varchar(50) OPEN My_Cursor FETCH NEXT FROM My_Cursor INTO @description WHILE @@FETCH_STATUS = 0 BEGIN INSERT INTO @RtnValue SELECT Data FROM dbo.Split(@description, @SplitOn) FETCH NEXT FROM My_Cursor INTO @description END CLOSE My_Cursor DEALLOCATE My_Cursor RETURN END SELECT DISTINCT Data FROM dbo.SplitAll(N' ') </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload