Published on

November 5, 2011

Exploring SQL Server: Solving Sudoku with T-SQL

Have you ever wondered if you can solve Sudoku puzzles using SQL Server? Well, I did, and I even wrote a script for it! In fact, I recently participated in the SQL Sudoku Challenge and earned a badge for my solution. How cool is that?

Let me share with you the script I used to solve the Sudoku puzzle. Here’s an example of the puzzle I solved:

53  7    6  195    98    6 8   6   34  8 3  17   2   6 6    28    419  5    8  79

The script takes the puzzle as input and solves it using T-SQL. It uses a recursive CTE (Common Table Expression) to iterate through the puzzle and fill in the missing numbers. The script also handles various constraints of the Sudoku puzzle, such as ensuring that each row, column, and 3×3 grid contains unique numbers.

Here’s a simplified version of the script:

DECLARE @SudokuGivens VARCHAR(100) = '53  7    6  195    98    6 8   6   34  8 3  17   2   6 6    28    419  5    8  79'

;WITH x( s, ind ) AS
( 
  SELECT @SudokuGivens, CHARINDEX(' ', @SudokuGivens) AS ind
  UNION ALL
  SELECT CONVERT(VARCHAR(100), SUBSTRING(s, 1, ind - 1) + CONVERT(CHAR(1), z) + SUBSTRING(s, ind + 1, 81)),
         CHARINDEX(' ', s, ind + 1) AS ind
  FROM x
    CROSS APPLY ( VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9)) z (z)
  WHERE ind > 0
  AND NOT EXISTS (
    SELECT NULL
    FROM ( VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9)) ww (lp)
    WHERE z = SUBSTRING(s, (ind - 1) % 9 - 8 + lp * 9, 1)
    OR    z = SUBSTRING(s, ((ind - 1) / 9) * 9 + lp, 1)
    OR    z = SUBSTRING(s, (((ind - 1) / 3) % 3) * 3 + (((ind - 1) / 27) * 27) + lp + (((lp - 1) / 3) * 6), 1)
  )
), Sud AS (
  SELECT TOP 9 SUBSTRING(s, ROW_NUMBER() OVER (ORDER BY s) * 9 - 8, ROW_NUMBER() OVER (ORDER BY s) * 9 - (ROW_NUMBER() OVER (ORDER BY s) * 9 - 9)) AS ConcatRow
  FROM x
  CROSS APPLY ( VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9)) y(N)
  WHERE ind = 0
)
SELECT c1.RowCol, [1], [2], [3], [4], [5], [6], [7], [8], [9]
FROM (
  SELECT S.RowCol, ColNo = ROW_NUMBER() OVER (PARTITION BY RowCol ORDER BY ConcatRow), Data = SUBSTRING(ConcatRow, ROW_NUMBER() OVER (PARTITION BY S.RowCol ORDER BY ConcatRow), 1)
  FROM @SudTable S
  CROSS APPLY ( VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9)) x(N)
) Intr
PIVOT (
  MAX(Data)
  FOR ColNo IN ([1], [2], [3], [4], [5], [6], [7], [8], [9])
) PVT) AS C1
WHERE c1.RowCol = S.RowCol
ORDER BY S.RowCol ASC

By running this script, you can solve Sudoku puzzles directly in SQL Server. You can even modify the script to handle multiple puzzles or use it as a stored procedure.

Although my solution is not the fastest, I’m excited to have earned a badge in the SQL Sudoku Challenge. It means I still have a lot to learn from other participants’ solutions. Solving Sudoku puzzles with T-SQL has been a fun and challenging experience, and I encourage you to give it a try!

Stay tuned for more SQL Server adventures and tips. Happy coding!

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.