Not Just Databases

  • Enter your email address to follow this blog and receive notifications of new posts by email.

  • Total Views

    • 732,258 hits
  • $riram $anka

    Unknown's avatar
    The experiences, Test cases, views, and opinions etc expressed in this website are my own and does not reflect the views or opinions of my employer. This site is independent of and does not represent Oracle Corporation in any way. Oracle does not officially sponsor, approve, or endorse this site or its content.Product and company names mentioned in this website may be the trademarks of their respective owners.

Solving Sudoku Using SQL – By Anton Scheffer

Posted by Sriram Sanka on September 12, 2022


Here is an Excellent piece of code that Gets the Sudoku Resolved.

with x( s, ind ) as
( select sud, instr( sud, ' ' )
  from ( select '&' sud from dual )
  union all
  select substr( s, 1, ind - 1 ) || z || substr( s, ind + 1 )
       , instr( s, ' ', ind + 1 )
  from x
     , ( select to_char( rownum ) z
         from dual
         connect by rownum <= 9
       ) z
  where ind > 0
  and not exists ( select null
                   from ( select rownum lp
                          from dual
                          connect by rownum <= 9
                        )
                   where z = substr( s, trunc( ( ind - 1 ) / 9 ) * 9 + lp, 1 )
                   or    z = substr( s, mod( ind - 1, 9 ) - 8 + lp * 9, 1 )
                   or    z = substr( s, mod( trunc( ( ind - 1 ) / 3 ), 3 ) * 3
                                      + trunc( ( ind - 1 ) / 27 ) * 27 + lp
                                      + trunc( ( lp - 1 ) / 3 ) * 6
                                   , 1 )
                 )
)
select line_wrap(s,9)
from x
where ind = 0
/

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.