It is relatively easy to implement by using T-SQL and XQuery. Specifically by using XQuery's quantified expressions.
Here is how it works:
- Converting input world list into XML, i.e. tokenization process.
- Running quantified expression. Sequential order of words is irrelevant.
- Counting number of words in the source and the target.
- Outcome of both (#2 and #3) is the final result.
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE( ID INT IDENTITY PRIMARY KEY, WordList1 VARCHAR(1024), WordList2 VARCHAR(1024));
INSERT INTO @tbl (WordList1, WordList2) VALUES
('one two three', 'two one three'),
('one two three', 'two one three '),
('one two three', ' one two two three three');
-- DDL and sample data population, end
DECLARE @Separator CHAR(1) = SPACE(1);
;WITH rs AS
(
SELECT *
, TRY_CAST('<root><source><r>' + REPLACE(WordList1, @Separator, '</r><r>') + '</r></source>'
+ '<target><r>' + REPLACE(WordList2, @Separator, '</r><r>') + '</r></target></root>' AS XML) AS xmldata
FROM @tbl
)
SELECT *
, xmldata.value('every $x in /root/source/r[text()]/text()
satisfies ($x = (/root/target/r[text()]/text())
and (count(/root/source/r[text()]) eq count(/root/target/r[text()])))', 'BIT') AS result
FROM rs;
Output
+----+-----------------+---------------------------+--------+
| ID | WordList1 | WordList2 | result |
+----+-----------------+---------------------------+--------+
| 1 | one two three | two one three | 1 |
| 2 | one two three | two one three | 1 |
| 3 | one two three | one two two three three | 0 |
+----+-----------------+---------------------------+--------+