--The solution to finding all possible six-character word-squares
--we fill this table with the possibilities at every stage
CREATE TABLE #possibilities(
MyID INT IDENTITY(1,1),
wordnumber INT,
string1 VARCHAR(6),
string2 VARCHAR(6),
string3 VARCHAR(6),
string4 VARCHAR(6),
string5 VARCHAR(6),
string6 VARCHAR(6),
word VARCHAR(6)
)
INSERT INTO #possibilities(wordnumber,string1, string2, string3, string4, string5, string6, word)
SELECT 1,
word,
SUBSTRING(word,2,1),
SUBSTRING(word,3,1),
SUBSTRING(word,4,1),
SUBSTRING(word,5,1),
SUBSTRING(word,6,1),
word
FROM wordlist w WHERE LEN(word)=6
CREATE INDEX idxWordNumber ON #possibilities(wordnumber)
INSERT INTO #possibilities(wordnumber,string1, string2, string3, string4, string5, string6, word)
SELECT 2,
e.word,
w.word,
e.string3+SUBSTRING(w.word,3,1),
e.string4+SUBSTRING(w.word,4,1),
e.string5+SUBSTRING(w.word,5,1),
e.string6+SUBSTRING(w.word,6,1),
w.word
FROM wordlist w
INNER JOIN #possibilities e
ON w.word LIKE e.string2+'%'
AND e.wordnumber=1
WHERE LEN(w.word)=6
AND EXISTS
(SELECT 1
FROM wordlist i
WHERE LEN(i.word)=6 AND i.word LIKE e.string3+ SUBSTRING(w.Word,3,1)+'%'
)
AND EXISTS
(SELECT 1
FROM wordlist j
WHERE LEN(j.word)=6 AND j.word LIKE e.string4+ SUBSTRING(w.Word,4,1)+'%'
)
AND EXISTS
(SELECT 1
FROM wordlist k
WHERE LEN(k.word)=6 AND k.word LIKE e.string5+ SUBSTRING(w.Word,5,1)+'%'
)
AND EXISTS
(SELECT 1
FROM wordlist l
WHERE LEN(l.word)=6 AND l.word LIKE e.string6+ SUBSTRING(w.Word,6,1)+'%'
)
INSERT INTO #possibilities(wordnumber,string1, string2, string3, string4, string5, string6, word)
SELECT 3,
e.string1,
e.string2,
w.word,
e.string4+SUBSTRING(w.word,4,1),
e.string5+SUBSTRING(w.word,5,1),
e.string6+SUBSTRING(w.word,6,1),
w.word
FROM wordlist w
INNER JOIN #possibilities e
ON w.word LIKE e.string3+'%'
AND e.wordnumber=2
WHERE LEN(w.word)=6
AND EXISTS
(SELECT 1
FROM wordlist j
WHERE LEN(j.word)=6 AND j.word LIKE e.string4+ SUBSTRING(w.Word,4,1)+'%'
)
AND EXISTS
(SELECT 1
FROM wordlist k
WHERE LEN(k.word)=6 AND k.word LIKE e.string5+ SUBSTRING(w.Word,5,1)+'%'
)
AND EXISTS
(SELECT 1
FROM wordlist l
WHERE LEN(l.word)=6 AND l.word LIKE e.string6+ SUBSTRING(w.Word,6,1)+'%'
)
INSERT INTO #possibilities(wordnumber,string1, string2, string3, string4, string5, string6, word)
SELECT 4,
e.string1,
e.string2,
e.string3,
w.word,
e.string5+SUBSTRING(w.word,5,1),
e.string6+SUBSTRING(w.word,6,1),
w.word
FROM wordlist w
INNER JOIN #possibilities e
ON w.word LIKE e.string4+'%'
AND e.wordnumber=3
WHERE LEN(w.word)=6
AND EXISTS
(SELECT 1
FROM wordlist k
WHERE LEN(k.word)=6 AND k.word LIKE e.string5+ SUBSTRING(w.Word,5,1)+'%'
)
AND EXISTS
(SELECT 1
FROM wordlist l
WHERE LEN(l.word)=6 AND l.word LIKE e.string6+ SUBSTRING(w.Word,6,1)+'%'
)
INSERT INTO #possibilities(wordnumber,string1, string2, string3, string4, string5, string6, word)
SELECT 5,
e.string1,
e.string2,
e.string3,
e.string4,
w.word,
e.string6+SUBSTRING(w.word,6,1),
w.word
FROM wordlist w
INNER JOIN #possibilities e
ON w.word LIKE e.string5+'%'
AND e.wordnumber=4
WHERE LEN(w.word)=6
AND EXISTS
(SELECT 1
FROM wordlist l
WHERE LEN(l.word)=6 AND l.word LIKE e.string6+ SUBSTRING(w.Word,6,1)+'%'
)
INSERT INTO #possibilities(wordnumber,string1, string2, string3, string4, string5, string6, word)
SELECT 6,
e.string1,
e.string2,
e.string3,
e.string4,
e.string5,
w.word,
w.word
FROM wordlist w
INNER JOIN #possibilities e
ON w.word LIKE e.string6+'%'
AND e.wordnumber=5
WHERE LEN(w.word)=6
---now let's create a permanent table for our solution
SELECT * INTO sixletterSquares FROM #possibilities WHERE wordnumber=6
SELECT * FROM sixLetterSquares