SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

SQL Puzzle: Advent of Code

A few years back someone started the Advent of code. If you aren’t familiar with it, it’s an advent calendar of coding puzzles. Each day of December before Christmas there are two puzzles (with very Christmasy descriptions). You can do them with any language you choose, or even multiple languages. I personally find it a challenge to do a coding puzzle with a language like SQL that’s really meant to be a batch language. That said, the first puzzle of the 2017 advent calendar is to take a long string of numbers and add up all of the ones where the next number is the same as the current one. So the ‘9991’ is 18, the first two count, the third one doesn’t because the next number is 1. ‘1234’ would be 0 because in no case is the next number the same as the current one. Oh, and it’s a loop. So the last number get’s compared with the first one.

So, here’s my variation. Construct your own random string of 200 digits, then calculate the total. Oh, and do your best to avoid loops. We like batches better don’t we? ??

The advent of code gets harder as it goes, but even at the beginning, it’s great practice in T-SQL, particularly if you are a developer.


My name is Kenneth Fisher and I am Senior DBA for a large (multi-national) insurance company. I have been working with databases for over 20 years starting with Clarion and Foxpro. I’ve been working with SQL Server for 12 years but have only really started “studying” the subject for the last 3. I don’t have any real "specialities" but I enjoy trouble shooting and teaching. Thus far I’ve earned by MCITP Database Administrator 2008, MCTS Database Administrator 2005, and MCTS Database Developer 2008. I’m currently studying for my MCITP Database Developer 2008 and should start in on the 2012 exams next year. My blog is at www.sqlstudies.com.


Leave a comment on the original post [sqlstudies.com, opens in a new window]

Loading comments...