## Solving SUDOKU thru SQL Code

 Author Message Jus Ten Centuries Group: General Forum Members Points: 1118 Visits: 275 Recently i cross through one article where it says the following SQL Code used in oracle to solve SUDOKU Puzzles.for 9x9 sudoku puzzles, the filled in numbers were passed as a string with a space for every unfilled square"in the following example, string passed is '53 7 6 195 98 6 8 6 34 8 3 17 2 6 6 28 419 5 8 79' ( One space for every unfilled square, if 3 continuous boxes are unfilled, then there should be 3 spaces)The following is the the code in Orcale used for the purpose.with x( s, ind ) as( select sud, instr( sud, ' ' ) from ( select '53 7 6 195 98 6 8 6 34 8 3 17 2 6 6 28 419 5 8 79' 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 sfrom xwhere ind = 0/I request our SQL experts to convert this code into SQL Server compatible code to solve sudoku puzzles.You can check the article about this @ http://technology.amis.nl/blog/6404/oracle-rdbms-11gr2-solving-a-sudoku-using-recursive-subquery-factoringRegardsJus Lynn Pettis SSC Guru Group: General Forum Members Points: 222522 Visits: 40373 Actually, I think you should give it a try first. If you run into problems, post what you have tried and we'll see what we can do to help you.This looks like a good training opportunity to learn how to port code. Always a good thing to know. Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) Jus Ten Centuries Group: General Forum Members Points: 1118 Visits: 275 Lynn..you are right..I should have tried first. But when i saw the code i found few oracle objects like dual , connect which am unaware of...hence posted..this was not for any project..just out of curiosity i posted this....if any one can post solution, i can try comparing oracle vs sql server codes and will learn from there........thanks SQLRNNR SSC Guru Group: General Forum Members Points: 143417 Visits: 18649 Late - but maybe still worth it. I'll be posting a blog on it shortly. Jason...AKA CirqueDeSQLeil_______________________________________________I have given a name to my pain...MCM SQL Server, MVPSQL RNNRPosting Performance Based Questions - Gail ShawLearn Extended Events weitzera SSCrazy Group: General Forum Members Points: 2204 Visits: 629 Just learned this yesterday, so I'll post a quick aside: Dual is a dummy object with one row.`select 'foo' from dual`is equivalent to the tsql statement `select 'foo'`which is invalid in ansi sql Dan Guzman - Not the MVP (7/22/2010)All questions have to be prefaced by Server version and 'according to MS Docs' or 'my own personal opinion based on how much detail I felt like digging into at the time.' SQLRNNR SSC Guru Group: General Forum Members Points: 143417 Visits: 18649 Blog post sneak peek. Jason...AKA CirqueDeSQLeil_______________________________________________I have given a name to my pain...MCM SQL Server, MVPSQL RNNRPosting Performance Based Questions - Gail ShawLearn Extended Events Lynn Pettis SSC Guru Group: General Forum Members Points: 222522 Visits: 40373 SQLRNNR (8/16/2011)Blog post sneak peek.Sneak peek didn't work, at least for me. Lynn PettisFor better assistance in answering your questions, click hereFor tips to get better help with Performance Problems, click hereFor Running Totals and its variations, click here or when working with partitioned tablesFor more about Tally Tables, click hereFor more about Cross Tabs and Pivots, click here and hereManaging Transaction LogsSQL Musings from the Desert Fountain Valley SQL (My Mirror Blog) SQLRNNR SSC Guru Group: General Forum Members Points: 143417 Visits: 18649 Lynn Pettis (8/16/2011)SQLRNNR (8/16/2011)Blog post sneak peek.Sneak peek didn't work, at least for me.Crud - I had hoped it would since it worked for me. It is scheduled to post 8/17 at 6AM (Pacific) Jason...AKA CirqueDeSQLeil_______________________________________________I have given a name to my pain...MCM SQL Server, MVPSQL RNNRPosting Performance Based Questions - Gail ShawLearn Extended Events nwerner SSC-Addicted Group: General Forum Members Points: 439 Visits: 1076 I don't care about solving SUDOKU with T-SQL. -- I like to do that myself. Does anyone have code to create guaranteed solvable SUDOKU puzzles? bc_ SSCrazy Group: General Forum Members Points: 2145 Visits: 7360 That is cool SQLRNNR.I don't know if this would always work, but this at least works for the given puzzle to return it in 9 x 9: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 - 8)) FROM x WHERE ind = 0 bc